Passed
Push — visitor ( 2d5566 )
by Akihito
11:02
created

SetterMethods   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 9
c 1
b 0
f 0
dl 0
loc 35
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 4 2
A add() 0 7 2
A accept() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Exception;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Ray\Di\Exception. 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...
8
9
final class SetterMethods
10
{
11
    /** @var SetterMethod[] */
12
    private $setterMethods;
13
14
    /**
15
     * @param array<SetterMethod> $setterMethods
16
     */
17
    public function __construct(array $setterMethods)
18
    {
19
        $this->setterMethods = $setterMethods;
20
    }
21
22
    /**
23
     * @throws Exception
24
     */
25
    public function __invoke(object $instance, Container $container): void
26
    {
27
        foreach ($this->setterMethods as $setterMethod) {
28
            ($setterMethod)($instance, $container);
29
        }
30
    }
31
32
    public function add(?SetterMethod $setterMethod = null): void
33
    {
34
        if (! $setterMethod) {
35
            return;
36
        }
37
38
        $this->setterMethods[] = $setterMethod;
39
    }
40
41
    public function accept(VisitorInterface $visitor)
42
    {
43
        $visitor->visitSetterMethods($this->setterMethods);
0 ignored issues
show
Bug introduced by
The method visitSetterMethods() does not exist on Ray\Di\VisitorInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        $visitor->/** @scrutinizer ignore-call */ 
44
                  visitSetterMethods($this->setterMethods);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
    }
45
}
46