Skip to content

Commit

Permalink
tweak: check method or property of object
Browse files Browse the repository at this point in the history
closes #489
  • Loading branch information
g105b committed Feb 29, 2024
1 parent 2ab9024 commit 951d9a3
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/BindableCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Gt\DomTemplate;

use Closure;
use Error;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionMethod;
Expand Down Expand Up @@ -70,7 +71,8 @@ public function isBindable(object $object):bool {

foreach($refAttributes as $refAttr) {
$bindKey = $this->getBindKey($refAttr, $refMethod);
$attributeCache[$bindKey] = fn(object $object):null|iterable|string => $this->nullableStringOrIterable($object->$methodName());
$attributeCache[$bindKey] = fn(object $object):null|iterable|string => $this->nullableStringOrIterable($object, $methodName);

if(class_exists($refReturnName)) {
$cacheObjectKeys[$bindKey] = $refReturnName;
}
Expand All @@ -85,7 +87,7 @@ public function isBindable(object $object):bool {
$bindKey = $this->getBindKey($refAttr);
// TODO: Test for object type in object property.
$attributeCache[$bindKey]
= fn(object $object, $key):null|iterable|string => $this->nullableStringOrIterable($object->$propName);
= fn(object $object, $key):null|iterable|string => $this->nullableStringOrIterable($object, $propName);
}
}
elseif($refProp->isPublic()) {
Expand All @@ -99,7 +101,7 @@ public function isBindable(object $object):bool {
$attributeCache[$bindKey] =
fn(object $object, $key):null|iterable|string =>
isset($object->$key)
? $this->nullableStringOrIterable($object->$key)
? $this->nullableStringOrIterable($object, $key)
: null;

if($refTypeName && class_exists($refTypeName)) {
Expand Down Expand Up @@ -192,7 +194,10 @@ public function convertToKvp(object|array $object):array {
$bindFunc = "get" . ucfirst($propName);
if($objectToExtract) {
if(property_exists($objectToExtract, $propName)) {
$objectToExtract = $objectToExtract->$propName;
try {
$objectToExtract = $objectToExtract->$propName;
}
catch(Error) {}

Check failure on line 200 in src/BindableCache.php

View workflow job for this annotation

GitHub Actions / phpstan

Dead catch - Error is never thrown in the try block.

Check warning on line 200 in src/BindableCache.php

View check run for this annotation

Codecov / codecov/patch

src/BindableCache.php#L200

Added line #L200 was not covered by tests
}
elseif(method_exists($objectToExtract, $bindFunc)) {
$objectToExtract = $objectToExtract->$bindFunc();
Expand Down Expand Up @@ -249,7 +254,17 @@ private function getBindKey(
}

/** @return null|string|array<int|string, mixed> */
private function nullableStringOrIterable(mixed $value):null|iterable|string {
private function nullableStringOrIterable(object $object, string $keyOrMethod):null|iterable|string {
if(method_exists($object, $keyOrMethod)) {
$value = $object->$keyOrMethod();
}
elseif(property_exists($object, $keyOrMethod)) {
$value = $object->$keyOrMethod;
}
else {
return null;

Check warning on line 265 in src/BindableCache.php

View check run for this annotation

Codecov / codecov/patch

src/BindableCache.php#L265

Added line #L265 was not covered by tests
}

if(is_scalar($value)) {
return $value;
}
Expand Down

0 comments on commit 951d9a3

Please sign in to comment.