Completed
Push — 2.x ( 450bf2...381469 )
by Akihito
18s queued 12s
created

AopClass::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Aop;
6
7
use PhpParser\BuilderFactory;
8
use PhpParser\Node\Identifier;
9
use PhpParser\Node\Name;
10
use PhpParser\Node\Stmt;
11
use PhpParser\Node\Stmt\Class_;
12
use PhpParser\Parser;
13
use ReflectionClass;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Ray\Aop\ReflectionClass.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
15
use function array_merge;
16
use function assert;
17
18
final class AopClass
19
{
20
    /** @var CodeGenMethod */
21
    private $codeGenMethod;
22
23
    /** @var AopClassName */
24
    private $aopClassName;
25
26
    /** @var AopProps */
27
    private $aopProps;
28
29
    public function __construct(
30
        Parser $parser,
31
        BuilderFactory $factory,
32
        AopClassName $aopClassName
33
    ) {
34
        $this->aopClassName = $aopClassName;
35
        $this->codeGenMethod = new CodeGenMethod($parser);
36
        $this->aopProps = new AopProps($factory);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @param ReflectionClass<object> $sourceClass
0 ignored issues
show
Documentation introduced by
The doc-type ReflectionClass<object> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
43
     */
44
    public function __invoke(CodeVisitor $visitor, ReflectionClass $sourceClass, BindInterface $bind): Class_
45
    {
46
        assert($visitor->class instanceof Class_);
47
        $methods = $this->codeGenMethod->getMethods($bind, $visitor);
48
        $propStms = ($this->aopProps)($sourceClass);
49
        $classStm = $visitor->class;
50
        $newClassName = ($this->aopClassName)((string) $visitor->class->name, $bind->toString(''));
51
        $classStm->name = new Identifier($newClassName);
52
        $classStm->extends = new Name('\\' . $sourceClass->name);
53
        $classStm->implements[] = new Name('WeavedInterface');
54
        /** @var array<int, Stmt> $stmts */
55
        $stmts = array_merge($propStms, $methods);
56
        $classStm->stmts = $stmts;
57
58
        return $classStm;
59
    }
60
}
61