Passed
Push — master ( 2f1f5f...dd46fb )
by Aleksei
06:02 queued 21s
created

Introspector::getAccessor()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.3731

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
ccs 5
cts 7
cp 0.7143
rs 10
cc 4
nc 3
nop 1
crap 4.3731
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Internal;
6
7
use Psr\Container\ContainerInterface;
8
use Spiral\Core\Container;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Spiral\Core\Internal\Container. 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...
9
use Spiral\Core\ContainerScope;
10
use Spiral\Core\Internal\Introspector\Accessor;
11
12
/**
13
 * @internal
14
 */
15
final class Introspector
16
{
17 1
    public static function scopeName(?ContainerInterface $container = null): ?string
18
    {
19 1
        return self::getAccessor($container)->scope->getScopeName();
20
    }
21
22
    /**
23
     * Returns list of scope names starting from the current scope to the root scope.
24
     *
25
     * @return list<string|null>
0 ignored issues
show
Bug introduced by
The type Spiral\Core\Internal\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
     */
27 1
    public static function scopeNames(?ContainerInterface $container = null): array
28
    {
29 1
        $scope = self::getAccessor($container)->scope;
30 1
        $result = [];
31
        do {
32 1
            $result[] = $scope->getScopeName();
33 1
            $scope = $scope->getParentScope();
34 1
        } while ($scope !== null);
35
36 1
        return $result;
37
    }
38
39
    /**
40
     * @psalm-assert Container|null $container
41
     */
42 2
    public static function getAccessor(?ContainerInterface $container = null): Accessor
43
    {
44 2
        if ($container !== null && !$container instanceof Container) {
45
            throw new \InvalidArgumentException('Container must be an instance of ' . Container::class);
46
        }
47
48 2
        $container ??= ContainerScope::getContainer();
49
50 2
        if (!$container instanceof Container) {
51
            throw new \RuntimeException('Container is not available.');
52
        }
53
54 2
        return new Accessor($container);
55
    }
56
}
57