AopClass   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
eloc 17
c 2
b 0
f 0
dl 0
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 15 1
A __construct() 0 8 1
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. Consider defining an alias.

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
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 list<Stmt> $stmts */
55
        $stmts = array_merge($propStms, $methods);
56
        $classStm->stmts = $stmts;
0 ignored issues
show
Documentation Bug introduced by
It seems like $stmts of type Ray\Aop\list is incompatible with the declared type PhpParser\Node\Stmt[] of property $stmts.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
58
        return $classStm;
59
    }
60
}
61