Completed
Push — master ( 2b0bc5...73caa0 )
by Kévin
9s
created

AnnotationDirectoryLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
3
/*
4
 * (c) Kévin Dunglas <[email protected]>
5
 *
6
 * This source file is subject to the MIT license that is bundled
7
 * with this source code in the file LICENSE.
8
 */
9
10
namespace Dunglas\ActionBundle\Routing;
11
12
use Symfony\Component\Config\FileLocatorInterface;
13
use Symfony\Component\Routing\Loader\AnnotationClassLoader;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Dunglas\ActionBundle\Routing\AnnotationClassLoader.

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
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader as BaseAnnotationDirectoryLoader;
15
16
/**
17
 * Overrides the supported type.
18
 *
19
 * @author Kévin Dunglas <[email protected]>
20
 */
21 View Code Duplication
class AnnotationDirectoryLoader extends BaseAnnotationDirectoryLoader
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
{
23
    private $scannedDirectories;
24
25
    public function __construct(FileLocatorInterface $locator, AnnotationClassLoader $loader, array $scannedDirectories)
26
    {
27
        parent::__construct($locator, $loader);
28
29
        $this->scannedDirectories = $scannedDirectories;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function supports($resource, $type = null)
36
    {
37
        if (!parent::supports($resource, $type)) {
38
            return false;
39
        }
40
41
        try {
42
            $path = $this->locator->locate($resource);
43
        } catch (\Exception $e) {
44
            return false;
45
        }
46
47
        return isset($this->scannedDirectories[rtrim($path, '/')]);
48
    }
49
}
50