Passed
Pull Request — master (#17)
by Mihail
15:10
created

AssertionTestSupportTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 22
c 2
b 0
f 1
dl 0
loc 53
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getObjectProperty() 0 11 2
A getObjectProperties() 0 24 3
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
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 ignore-type  annotation

22
            return $property->getValue(/** @scrutinizer ignore-type */ $objectOrString);
Loading history...
23
        } catch (\ReflectionException $e) {
24
            $this->markTestSkipped('[Reflection Error: ' . $e->getMessage());
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

24
            $this->/** @scrutinizer ignore-call */ 
25
                   markTestSkipped('[Reflection Error: ' . $e->getMessage());
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
Bug introduced by
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 ignore-type  annotation

53
                return $property->getValue(/** @scrutinizer ignore-type */ $objectOrString);
Loading history...
54
            }, $properties));
55
56
        } catch (\ReflectionException $e) {
57
            $this->markTestSkipped('[Reflection Error: ' . $e->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
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