Skip to content

Commit

Permalink
Fix PSR-2 violations
Browse files Browse the repository at this point in the history
  • Loading branch information
josegonzalez committed Dec 2, 2015
1 parent f396ff1 commit b3783a2
Show file tree
Hide file tree
Showing 4 changed files with 423 additions and 391 deletions.
138 changes: 74 additions & 64 deletions src/AccessibilityHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,59 @@
*
* @copyright Christian Winther, 2013
*/
trait AccessibilityHelperTrait {

trait AccessibilityHelperTrait
{
/**
* Default target to use for reflection.
*
* @var Object|string
*/
public $defaultReflectionTarget = null;
public $defaultReflectionTarget = null;

/**
* List of Reflection properties made public.
*
* @var array
*/
protected $_reflectionPropertyCache = [];
protected $_reflectionPropertyCache = [];

/**
* List of Reflection methods made public.
*
* @var array
*/
protected $_reflectionMethodCache = [];
protected $_reflectionMethodCache = [];

/**
* List of class names <=> instances used for invocation.
*
* @var array
*/
protected $_reflectionInstanceCache = [];
protected $_reflectionInstanceCache = [];

/**
* Reset the internal reflection caches.
*
* @return void
*/
public function resetReflectionCache() {
$this->_reflectionPropertyCache = [];
$this->_reflectionMethodCache = [];
$this->_reflectionInstanceCache = [];
}
public function resetReflectionCache()
{
$this->_reflectionPropertyCache = [];
$this->_reflectionMethodCache = [];
$this->_reflectionInstanceCache = [];
}

/**
* Map an instance of an object to its class name.
*
* @param Object $instance
* @return void
*/
public function setReflectionClassInstance($instance, $class = null) {
$class = $class ?: get_class($instance);
$this->_reflectionInstanceCache[$class] = $instance;
}
public function setReflectionClassInstance($instance, $class = null)
{
$class = $class ?: get_class($instance);
$this->_reflectionInstanceCache[$class] = $instance;
}

/**
* Get working instance of "$class".
Expand All @@ -72,14 +74,15 @@ public function setReflectionClassInstance($instance, $class = null) {
* @return Object
* @throws \Exception
*/
public function getReflectionInstance($class) {
$class = $this->_getReflectionTargetClass($class);
if (empty($this->_reflectionInstanceCache[$class])) {
throw new Exception(sprintf('Unable to find instance of %s in the reflection cache. Have you added it using "setReflectionClassInstance"?', $class));
}
public function getReflectionInstance($class)
{
$class = $this->_getReflectionTargetClass($class);
if (empty($this->_reflectionInstanceCache[$class])) {
throw new Exception(sprintf('Unable to find instance of %s in the reflection cache. Have you added it using "setReflectionClassInstance"?', $class));
}

return $this->_reflectionInstanceCache[$class];
}
return $this->_reflectionInstanceCache[$class];
}

/**
* Helper method to call a protected method.
Expand All @@ -89,17 +92,18 @@ public function getReflectionInstance($class) {
* @param string $class Target reflection class
* @return mixed
*/
public function callProtectedMethod($method, $args = [], $class = null) {
$class = $this->_getReflectionTargetClass($class);
$cacheKey = $class . '_' . $method;
public function callProtectedMethod($method, $args = [], $class = null)
{
$class = $this->_getReflectionTargetClass($class);
$cacheKey = $class . '_' . $method;

if (!isset($this->_reflectionMethodCache[$cacheKey])) {
$this->_reflectionMethodCache[$cacheKey] = $this->_getNewReflectionMethod($class, $method);
$this->_reflectionMethodCache[$cacheKey]->setAccessible(true);
}
if (!isset($this->_reflectionMethodCache[$cacheKey])) {
$this->_reflectionMethodCache[$cacheKey] = $this->_getNewReflectionMethod($class, $method);
$this->_reflectionMethodCache[$cacheKey]->setAccessible(true);
}

return $this->_reflectionMethodCache[$cacheKey]->invokeArgs($this->getReflectionInstance($class), $args);
}
return $this->_reflectionMethodCache[$cacheKey]->invokeArgs($this->getReflectionInstance($class), $args);
}

/**
* Helper method to get the value of a protected property.
Expand All @@ -108,10 +112,11 @@ public function callProtectedMethod($method, $args = [], $class = null) {
* @param string $class Target reflection class
* @return mixed
*/
public function getProtectedProperty($property, $class = null) {
$Instance = $this->_getReflectionPropertyInstance($property, $class);
return $Instance->getValue($this->getReflectionInstance($class));
}
public function getProtectedProperty($property, $class = null)
{
$Instance = $this->_getReflectionPropertyInstance($property, $class);
return $Instance->getValue($this->getReflectionInstance($class));
}

/**
* Helper method to set the value of a protected property.
Expand All @@ -121,10 +126,11 @@ public function getProtectedProperty($property, $class = null) {
* @param string $class Target reflection class
* @return mixed
*/
public function setProtectedProperty($property, $value, $class = null) {
$Instance = $this->_getReflectionPropertyInstance($property, $class);
return $Instance->setValue($this->getReflectionInstance($class), $value);
}
public function setProtectedProperty($property, $value, $class = null)
{
$Instance = $this->_getReflectionPropertyInstance($property, $class);
return $Instance->setValue($this->getReflectionInstance($class), $value);
}

/**
* Get a reflection property object.
Expand All @@ -133,17 +139,18 @@ public function setProtectedProperty($property, $value, $class = null) {
* @param string $class
* @return \ReflectionProperty
*/
protected function _getReflectionPropertyInstance($property, $class) {
$class = $this->_getReflectionTargetClass($class);
$cacheKey = $class . '_' . $property;
protected function _getReflectionPropertyInstance($property, $class)
{
$class = $this->_getReflectionTargetClass($class);
$cacheKey = $class . '_' . $property;

if (!isset($this->_reflectionPropertyCache[$cacheKey])) {
$this->_reflectionPropertyCache[$cacheKey] = $this->_getNewReflectionProperty($class, $property);
$this->_reflectionPropertyCache[$cacheKey]->setAccessible(true);
}
if (!isset($this->_reflectionPropertyCache[$cacheKey])) {
$this->_reflectionPropertyCache[$cacheKey] = $this->_getNewReflectionProperty($class, $property);
$this->_reflectionPropertyCache[$cacheKey]->setAccessible(true);
}

return $this->_reflectionPropertyCache[$cacheKey];
}
return $this->_reflectionPropertyCache[$cacheKey];
}

/**
* Get the reflection class name.
Expand All @@ -152,19 +159,20 @@ protected function _getReflectionPropertyInstance($property, $class) {
* @return string
* @throws \Exception
*/
protected function _getReflectionTargetClass($class) {
$class = $class ?: $this->defaultReflectionTarget;
protected function _getReflectionTargetClass($class)
{
$class = $class ?: $this->defaultReflectionTarget;

if (!$class) {
throw new Exception('Unable to find reflection target; have you set $defaultReflectionTarget or passed in a class name?');
}
if (!$class) {
throw new Exception('Unable to find reflection target; have you set $defaultReflectionTarget or passed in a class name?');
}

if (!is_object($class)) {
return $class;
}
if (!is_object($class)) {
return $class;
}

return get_class($class);
}
return get_class($class);
}

/**
* Gets a new ReflectionMethod instance. Extracted for testing purposes.
Expand All @@ -173,9 +181,10 @@ protected function _getReflectionTargetClass($class) {
* @param string $method
* @return \ReflectionMethod
*/
protected function _getNewReflectionMethod($class, $method) {
return new ReflectionMethod($class, $method);
}
protected function _getNewReflectionMethod($class, $method)
{
return new ReflectionMethod($class, $method);
}

/**
* Gets a new ReflectionProperty instance. Extracted for testing purposes.
Expand All @@ -184,7 +193,8 @@ protected function _getNewReflectionMethod($class, $method) {
* @param string $property
* @return \ReflectionProperty
*/
protected function _getNewReflectionProperty($class, $property) {
return new ReflectionProperty($class, $property);
}
protected function _getNewReflectionProperty($class, $property)
{
return new ReflectionProperty($class, $property);
}
}
28 changes: 15 additions & 13 deletions src/CounterHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
*
* @copyright Andy Dawson, 2013
*/
trait CounterHelperTrait {
trait CounterHelperTrait
{

/**
* List of counters used by this test case
*
* @var array
*/
protected $_expectationCounters = [];
protected $_expectationCounters = [];

/**
* Returns a matcher that matches when the method it is evaluated for
Expand All @@ -27,15 +28,16 @@ trait CounterHelperTrait {
* @param mixed $name string or object
* @return \PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex
*/
public function next($name = '') {
if (is_object($name)) {
$name = get_class($name);
}
if (!isset($this->_expectationCounters[$name])) {
$this->_expectationCounters[$name] = 0;
} else {
$this->_expectationCounters[$name] += 1;
}
return $this->at($this->_expectationCounters[$name]);
}
public function next($name = '')
{
if (is_object($name)) {
$name = get_class($name);
}
if (!isset($this->_expectationCounters[$name])) {
$this->_expectationCounters[$name] = 0;
} else {
$this->_expectationCounters[$name] += 1;
}
return $this->at($this->_expectationCounters[$name]);
}
}
Loading

0 comments on commit b3783a2

Please sign in to comment.