Completed
Pull Request — master (#671)
by Antonio
05:38 queued 02:33
created

RelativeNamespaceDiscovery   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 91
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setRelativeNamespace() 0 6 1
B getClasses() 0 24 4
A getFile() 0 4 1
A search() 0 9 1
A convertPathToNamespace() 0 4 1
A convertNamespaceToPath() 0 4 1
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_map(function ($directory) use ($relativePath) {
45
                return $directory.$relativePath;
46
            }, $directories);
47
48
            $directories = array_filter($directories, function ($path) {
49
                return is_dir($path);
50
            });
51
52
            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...
53
                foreach ($this->search($directories, $this->searchPattern) as $file) {
54
                    $relativePathName = $file->getRelativePathname();
55
                    $classes[] = $baseNamespace.$this->convertPathToNamespace($relativePath.DIRECTORY_SEPARATOR.$relativePathName);
56
                }
57
            }
58
        }
59
60
        return $classes;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getFile($class)
67
    {
68
        return $this->getClassLoader()->findFile($class);
69
    }
70
71
    /**
72
     * @param $directories
73
     * @param $pattern
74
     *
75
     * @return \Symfony\Component\Finder\Finder
76
     */
77
    protected function search($directories, $pattern)
78
    {
79
        $finder = new Finder();
80
        $finder->files()
81
          ->name($pattern)
82
          ->in($directories);
83
84
        return $finder;
85
    }
86
87
    /**
88
     * @param $path
89
     *
90
     * @return mixed
91
     */
92
    protected function convertPathToNamespace($path)
93
    {
94
        return str_replace([DIRECTORY_SEPARATOR, '.php'], ['\\', ''], trim($path, DIRECTORY_SEPARATOR));
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function convertNamespaceToPath($namespace)
101
    {
102
        return DIRECTORY_SEPARATOR.str_replace("\\", DIRECTORY_SEPARATOR, trim($namespace, '\\'));
103
    }
104
}
105