kodedphp /
http
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace Tests\Koded\Http; |
||||||
| 4 | |||||||
| 5 | trait AssertionTestSupportTrait |
||||||
| 6 | { |
||||||
| 7 | /** |
||||||
| 8 | * @param object|string $objectOrString |
||||||
| 9 | * @param string $propertyName |
||||||
| 10 | * |
||||||
| 11 | * @return mixed |
||||||
| 12 | * @throws \ReflectionException |
||||||
| 13 | */ |
||||||
| 14 | private function getObjectProperty( |
||||||
| 15 | object|string $objectOrString, |
||||||
| 16 | string $propertyName): mixed |
||||||
| 17 | { |
||||||
| 18 | try { |
||||||
| 19 | $proto = new \ReflectionClass($objectOrString); |
||||||
| 20 | $property = $proto->getProperty($propertyName); |
||||||
| 21 | $property->setAccessible(true); |
||||||
| 22 | return $property->getValue($objectOrString); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 23 | } catch (\ReflectionException $e) { |
||||||
| 24 | $this->markTestSkipped('[Reflection Error: ' . $e->getMessage()); |
||||||
|
0 ignored issues
–
show
It seems like
markTestSkipped() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 25 | } |
||||||
| 26 | } |
||||||
| 27 | |||||||
| 28 | /** |
||||||
| 29 | * @param object|string $objectOrString |
||||||
| 30 | * @param array $propertyNames List of property names, or empty for all properties |
||||||
| 31 | * |
||||||
| 32 | * @return array |
||||||
| 33 | */ |
||||||
| 34 | private function getObjectProperties( |
||||||
| 35 | object|string $objectOrString, |
||||||
| 36 | array $propertyNames = []): array |
||||||
| 37 | { |
||||||
| 38 | try { |
||||||
| 39 | $properties = (new \ReflectionClass($objectOrString))->getProperties(); |
||||||
| 40 | |||||||
| 41 | if (count($propertyNames) > 0) { |
||||||
| 42 | $properties = array_filter($properties, function(\ReflectionProperty $property) use ($propertyNames) { |
||||||
| 43 | return in_array($property->getName(), $propertyNames); |
||||||
| 44 | }); |
||||||
| 45 | } |
||||||
| 46 | |||||||
| 47 | $propertyKeys = array_map(function(\ReflectionProperty $property) { |
||||||
| 48 | return $property->getName(); |
||||||
| 49 | }, $properties); |
||||||
| 50 | |||||||
| 51 | return array_combine($propertyKeys, array_map(function(\ReflectionProperty $property) use($objectOrString) { |
||||||
| 52 | $property->setAccessible(true); |
||||||
| 53 | return $property->getValue($objectOrString); |
||||||
|
0 ignored issues
–
show
It seems like
$objectOrString can also be of type string; however, parameter $object of ReflectionProperty::getValue() does only seem to accept null|object, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 54 | }, $properties)); |
||||||
| 55 | |||||||
| 56 | } catch (\ReflectionException $e) { |
||||||
| 57 | $this->markTestSkipped('[Reflection Error: ' . $e->getMessage()); |
||||||
|
0 ignored issues
–
show
In this branch, the function will implicitly return
null which is incompatible with the type-hinted return array. Consider adding a return statement or allowing null as return value.
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: interface ReturnsInt {
public function returnsIntHinted(): int;
}
class MyClass implements ReturnsInt {
public function returnsIntHinted(): int
{
if (foo()) {
return 123;
}
// here: null is implicitly returned
}
}
Loading history...
|
|||||||
| 58 | } |
||||||
| 59 | } |
||||||
| 60 | } |
||||||
| 61 |