Skip to content

Commit

Permalink
Merge pull request #131 from ruudk/expression-visitor
Browse files Browse the repository at this point in the history
ClosureExpressionVisitor > Don't duplicate the accessor when the field already starts with it
  • Loading branch information
alcaeus authored Mar 5, 2019
2 parents 555e82d + f43cf44 commit a99d26d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use function is_array;
use function iterator_to_array;
use function method_exists;
use function preg_match;
use function preg_replace_callback;
use function strlen;
use function strpos;
Expand Down Expand Up @@ -44,11 +45,13 @@ public static function getObjectFieldValue($object, $field)
foreach ($accessors as $accessor) {
$accessor .= $field;

if (! method_exists($object, $accessor)) {
continue;
if (method_exists($object, $accessor)) {
return $object->$accessor();
}
}

return $object->$accessor();
if (preg_match('/^is[A-Z]+/', $field) === 1 && method_exists($object, $field)) {
return $object->$field();
}

// __call should be triggered for get.
Expand All @@ -74,11 +77,9 @@ public static function getObjectFieldValue($object, $field)
foreach ($accessors as $accessor) {
$accessor .= $ccField;

if (! method_exists($object, $accessor)) {
continue;
if (method_exists($object, $accessor)) {
return $object->$accessor();
}

return $object->$accessor();
}

return $object->$field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public function testGetObjectFieldValueIsAccessor() : void
self::assertTrue($this->visitor->getObjectFieldValue($object, 'baz'));
}

public function testGetObjectFieldValueIsAccessorWithIsPrefix() : void
{
$object = new TestObject(1, 2, true);

self::assertTrue($this->visitor->getObjectFieldValue($object, 'isBaz'));
}

public function testGetObjectFieldValueIsAccessorCamelCase() : void
{
$object = new TestObjectNotCamelCase(1);
Expand Down

0 comments on commit a99d26d

Please sign in to comment.