Skip to content

Commit

Permalink
Don't extract getters of objects
Browse files Browse the repository at this point in the history
While extracting getters for objects is a nice idea in theory,
in practice the whole idea of calling random get* and is* methods
on objects can backfire, because we can actually hide, or even worse,
accidentally cause calling methods that shouldn't be called in the
first place.

Fixes mockery#619
  • Loading branch information
robertbasic committed May 9, 2017
1 parent ceb487b commit 8addb2b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Marked `Mockery\MockInterface` as internal
* Subset matcher matches recusively
* BC BREAK - Spies return `null` by default from ignored (non-mocked) methods with nullable return type
* Removed extracting getter methods of object instances

## 0.9.4 (XXXX-XX-XX)

Expand Down
37 changes: 1 addition & 36 deletions library/Mockery.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,7 @@ private static function objectToArray($object, $nesting = 3)

return array(
'class' => get_class($object),
'properties' => self::extractInstancePublicProperties($object, $nesting),
'getters' => self::extractGetters($object, $nesting)
'properties' => self::extractInstancePublicProperties($object, $nesting)
);
}

Expand Down Expand Up @@ -600,40 +599,6 @@ private static function extractInstancePublicProperties($object, $nesting)
return $cleanedProperties;
}

/**
* Returns all object getters.
*
* @param $object
* @param $nesting
*
* @return array
*/
private static function extractGetters($object, $nesting)
{
$reflection = new \ReflectionClass(get_class($object));
$publicMethods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
$getters = array();

foreach ($publicMethods as $publicMethod) {
$name = $publicMethod->getName();
$irrelevantName = (substr($name, 0, 3) !== 'get' && substr($name, 0, 2) !== 'is');
$isStatic = $publicMethod->isStatic();
$numberOfParameters = $publicMethod->getNumberOfParameters();

if ($irrelevantName || $numberOfParameters != 0 || $isStatic) {
continue;
}

try {
$getters[$name] = self::cleanupNesting($object->$name(), $nesting);
} catch (\Exception $e) {
$getters[$name] = '!! ' . get_class($e) . ': ' . $e->getMessage() . ' !!';
}
}

return $getters;
}

/**
* Utility method used for recursively generating
* an object or array representation.
Expand Down

0 comments on commit 8addb2b

Please sign in to comment.