Completed
Push — master ( 2bbfc9...9b6b62 )
by Marco
12s
created

Visitor/HydratorMethodsVisitorTest.php (3 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace GeneratedHydratorTest\CodeGenerator\Visitor;
6
7
use GeneratedHydrator\CodeGenerator\Visitor\HydratorMethodsVisitor;
8
use PhpParser\Node;
9
use PhpParser\Node\Expr\Assign;
10
use PhpParser\Node\Expr\PropertyFetch;
11
use PhpParser\Node\Stmt\Class_;
12
use PhpParser\Node\Stmt\ClassMethod;
13
use PhpParser\ParserFactory;
14
use PHPUnit\Framework\TestCase;
15
use CodeGenerationUtils\Inflector\Util\UniqueIdentifierGenerator;
16
use ReflectionClass;
17
18
/**
19
 * Tests for {@see \GeneratedHydrator\CodeGenerator\Visitor\HydratorMethodsVisitor}
20
 *
21
 * @author Marco Pivetta <[email protected]>
22
 * @license MIT
23
 *
24
 * @covers \GeneratedHydrator\CodeGenerator\Visitor\HydratorMethodsVisitor
25
 */
26
class HydratorMethodsVisitorTest extends TestCase
27
{
28
    /**
29
     * @dataProvider classAstProvider
30
     *
31
     * @param string   $className
32
     * @param Class_   $classNode
33
     * @param string[] $properties
34
     */
35
    public function testBasicCodeGeneration(string $className, Class_ $classNode, array $properties)
36
    {
37
        $visitor = new HydratorMethodsVisitor(new ReflectionClass($className));
38
39
        /* @var $modifiedAst Class_ */
40
        $modifiedNode = $visitor->leaveNode($classNode);
41
42
        self::assertMethodExistence('hydrate', $modifiedNode);
0 ignored issues
show
It seems like $modifiedNode defined by $visitor->leaveNode($classNode) on line 40 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...
43
        self::assertMethodExistence('extract', $modifiedNode);
0 ignored issues
show
It seems like $modifiedNode defined by $visitor->leaveNode($classNode) on line 40 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...
44
        self::assertMethodExistence('__construct', $modifiedNode);
0 ignored issues
show
It seems like $modifiedNode defined by $visitor->leaveNode($classNode) on line 40 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...
45
    }
46
47
    /**
48
     * Verifies that a method was correctly added to by the visitor
49
     *
50
     * @param string $methodName
51
     * @param Class_ $class
52
     */
53
    private function assertMethodExistence(string $methodName, Class_ $class)
54
    {
55
        $members = $class->stmts;
56
57
        self::assertCount(
58
            1,
59
            array_filter(
60
                $members,
61
                function (Node $node) use ($methodName) : bool {
62
                    return $node instanceof ClassMethod
63
                        && $methodName === $node->name;
64
                }
65
            )
66
        );
67
    }
68
69
    /**
70
     * @return \PhpParser\Node[][]
71
     */
72
    public function classAstProvider() : array
73
    {
74
        $parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
75
76
        $className = UniqueIdentifierGenerator::getIdentifier('Foo');
77
        $classCode = 'class ' . $className . ' { private $bar; private $baz; protected $tab; '
78
            . 'protected $tar; public $taw; public $tam; }';
79
80
        eval($classCode);
81
82
        $staticClassName = UniqueIdentifierGenerator::getIdentifier('Foo');
83
        $staticClassCode = 'class ' . $staticClassName . ' { private static $bar; '
84
            . 'protected static $baz; public static $tab; private $taz; }';
85
86
        eval($staticClassCode);
87
88
        return [
89
            [$className, $parser->parse('<?php ' . $classCode)[0], ['bar', 'baz', 'tab', 'tar', 'taw', 'tam']],
90
            [$staticClassName, $parser->parse('<?php ' . $staticClassCode)[0], ['taz']],
91
        ];
92
    }
93
}
94