assertMethodExistence()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GeneratedHydratorTest\CodeGenerator\Visitor;
6
7
use CodeGenerationUtils\Inflector\Util\UniqueIdentifierGenerator;
8
use GeneratedHydrator\CodeGenerator\Visitor\HydratorMethodsVisitor;
9
use PhpParser\Node;
10
use PhpParser\Node\Stmt\Class_;
11
use PhpParser\Node\Stmt\ClassMethod;
12
use PhpParser\ParserFactory;
13
use PHPUnit\Framework\TestCase;
14
use ReflectionClass;
15
use function array_filter;
16
17
/**
18
 * Tests for {@see \GeneratedHydrator\CodeGenerator\Visitor\HydratorMethodsVisitor}
19
 *
20
 * @covers \GeneratedHydrator\CodeGenerator\Visitor\HydratorMethodsVisitor
21
 */
22
class HydratorMethodsVisitorTest extends TestCase
23
{
24
    /**
25
     * @param string[] $properties
26
     *
27
     * @dataProvider classAstProvider
28
     */
29
    public function testBasicCodeGeneration(string $className, Class_ $classNode, array $properties) : void
0 ignored issues
show
Unused Code introduced by
The parameter $properties is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
    {
31
        $visitor = new HydratorMethodsVisitor(new ReflectionClass($className));
32
33
        /** @var Class_ $modifiedAst */
34
        $modifiedNode = $visitor->leaveNode($classNode);
35
36
        self::assertMethodExistence('hydrate', $modifiedNode);
0 ignored issues
show
Bug introduced by
It seems like $modifiedNode defined by $visitor->leaveNode($classNode) on line 34 can be null; however, GeneratedHydratorTest\Co...assertMethodExistence() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
37
        self::assertMethodExistence('extract', $modifiedNode);
0 ignored issues
show
Bug introduced by
It seems like $modifiedNode defined by $visitor->leaveNode($classNode) on line 34 can be null; however, GeneratedHydratorTest\Co...assertMethodExistence() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
38
        self::assertMethodExistence('__construct', $modifiedNode);
0 ignored issues
show
Bug introduced by
It seems like $modifiedNode defined by $visitor->leaveNode($classNode) on line 34 can be null; however, GeneratedHydratorTest\Co...assertMethodExistence() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
39
    }
40
41
    /**
42
     * Verifies that a method was correctly added to by the visitor
43
     */
44
    private function assertMethodExistence(string $methodName, Class_ $class) : void
45
    {
46
        $members = $class->stmts;
47
48
        self::assertCount(
49
            1,
50
            array_filter(
0 ignored issues
show
Documentation introduced by
\array_filter($members, ...= $node->name->name; }) is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
                $members,
52
                static function (Node $node) use ($methodName) : bool {
53
                    return $node instanceof ClassMethod
54
                        && $methodName === $node->name->name;
55
                }
56
            )
57
        );
58
    }
59
60
    /**
61
     * @return Node[]
62
     */
63
    public function classAstProvider() : array
64
    {
65
        $parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
66
67
        $className = UniqueIdentifierGenerator::getIdentifier('Foo');
68
        $classCode = 'class ' . $className . ' { private $bar; private $baz; protected $tab; '
69
            . 'protected $tar; public $taw; public $tam; }';
70
71
        eval($classCode);
72
73
        $staticClassName = UniqueIdentifierGenerator::getIdentifier('Foo');
74
        $staticClassCode = 'class ' . $staticClassName . ' { private static $bar; '
75
            . 'protected static $baz; public static $tab; private $taz; }';
76
77
        eval($staticClassCode);
78
79
        return [
80
            [$className, $parser->parse('<?php ' . $classCode)[0], ['bar', 'baz', 'tab', 'tar', 'taw', 'tam']],
81
            [$staticClassName, $parser->parse('<?php ' . $staticClassCode)[0], ['taz']],
82
        ];
83
    }
84
}
85