AbstractTraitTestCase   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A assertObjectUsesTrait() 0 14 2
A getTraits() 0 8 2
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Tests\Unit\Dms\MySQL\QueryBuilder\Traits;
4
5
use PHPUnit\Framework\TestCase;
6
7
abstract class AbstractTraitTestCase extends TestCase
8
{
9
    /**
10
     * @param string $trait
11
     * @param string|object $object
12
     * @param string $message
13
     */
14
    protected function assertObjectUsesTrait(string $trait, $object, string $message = '')
15
    {
16
        $traits = [];
17
        $this->getTraits($object, $traits);
18
19
        parent::assertThat(
20
            \in_array($trait, $traits),
21
            static::isTrue(),
22
            \mb_strlen($message)
23
                ? $message
24
                : \sprintf(
25
                    'Failed asserting that "%s" is used in object "%s"',
26
                    $trait,
27
                    \get_class($object)
0 ignored issues
show
Bug introduced by
It seems like $object can also be of type string; however, parameter $object of get_class() does only seem to accept 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

27
                    \get_class(/** @scrutinizer ignore-type */ $object)
Loading history...
28
                )
29
        );
30
    }
31
32
    /**
33
     * @param string|object $class
34
     * @param array $traits
35
     */
36
    private function getTraits($class, array &$traits)
37
    {
38
        $classTraits = \array_keys((new \ReflectionClass($class))->getTraits());
39
40
        $traits = \array_merge($traits, $classTraits);
41
42
        foreach ($classTraits as $classTrait) {
43
            $this->getTraits($classTrait, $traits);
44
        }
45
    }
46
}
47