Completed
Pull Request — master (#671)
by Antonio
03:03
created

RelativeNamespaceDiscovery::getClasses()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.2
cc 4
eloc 12
nc 4
nop 0
1
<?php
2
3
namespace Robo\ClassDiscovery;
4
5
use Robo\Common\ClassLoaderAwareTrait;
6
use Robo\Contract\ClassLoaderAwareInterface;
7
use Symfony\Component\Finder\Finder;
8
9
/**
10
 * Class RelativeNamespaceDiscovery
11
 *
12
 * @package Robo\Plugin\ClassDiscovery
13
 */
14
class RelativeNamespaceDiscovery extends AbstractClassDiscovery implements ClassLoaderAwareInterface
15
{
16
    use ClassLoaderAwareTrait;
17
18
    /**
19
     * @var string
20
     */
21
    protected $relativeNamespace = '';
22
23
    /**
24
     * @param string $relativeNamespace
25
     *
26
     * @return RelativeNamespaceDiscovery
27
     */
28
    public function setRelativeNamespace($relativeNamespace)
29
    {
30
        $this->relativeNamespace = $relativeNamespace;
31
32
        return $this;
33
    }
34
35
    /**
36
     * @inheritDoc
37
     */
38
    public function getClasses()
39
    {
40
        $classes = [];
41
        $relativePath = $this->convertNamespaceToPath($this->relativeNamespace);
42
43
        foreach ($this->getClassLoader()->getPrefixesPsr4() as $baseNamespace => $directories) {
44
            $directories = array_filter(array_map(function ($directory) use ($relativePath) {
45
                return $directory.$relativePath;
46
            }, $directories), 'is_dir');
47
48
            if ($directories) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $directories of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
49
                foreach ($this->search($directories, $this->searchPattern) as $file) {
50
                    $relativePathName = $file->getRelativePathname();
51
                    $classes[] = $baseNamespace.$this->convertPathToNamespace($relativePath.DIRECTORY_SEPARATOR.$relativePathName);
52
                }
53
            }
54
        }
55
56
        return $classes;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getFile($class)
63
    {
64
        return $this->getClassLoader()->findFile($class);
65
    }
66
67
    /**
68
     * @param $directories
69
     * @param $pattern
70
     *
71
     * @return \Symfony\Component\Finder\Finder
72
     */
73
    protected function search($directories, $pattern)
74
    {
75
        $finder = new Finder();
76
        $finder->files()
77
          ->name($pattern)
78
          ->in($directories);
79
80
        return $finder;
81
    }
82
83
    /**
84
     * @param $path
85
     *
86
     * @return mixed
87
     */
88
    protected function convertPathToNamespace($path)
89
    {
90
        return str_replace([DIRECTORY_SEPARATOR, '.php'], ['\\', ''], trim($path, DIRECTORY_SEPARATOR));
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function convertNamespaceToPath($namespace)
97
    {
98
        return DIRECTORY_SEPARATOR.str_replace("\\", DIRECTORY_SEPARATOR, trim($namespace, '\\'));
99
    }
100
}
101