GetNamespacesFromFiles::execute()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 24
rs 9.8666
1
<?php
2
3
namespace MagentoHackathon\Service;
4
5
use MagentoHackathon\Model\FileList;
6
use Symfony\Component\Finder\SplFileInfo;
7
8
class GetNamespacesFromFiles
9
{
10
    /**
11
     * @param FileList $fileList
12
     * @param string $moduleNameSpaceStart
13
     * @return array
14
     */
15
    public function execute(FileList $fileList, $moduleNameSpaceStart = '')
16
    {
17
        $filePhpFiles = array_merge($fileList->getPhpFileList(), $fileList->getTemplates());
18
        $namespacesFromFiles = [];
19
20
        /** @var SplFileInfo $file */
21
        foreach ($filePhpFiles as $file) {
22
            $pattern = '/([A-z0-9]+' . preg_quote('\\', '/') . '){2}/';
23
24
            preg_match_all($pattern, $file->getContents(), $matches);
25
26
            foreach ($matches[0] as $match) {
27
                $removeLeadingSlash = ltrim($match, '\\');
28
                $splitNamespace = explode('\\', $removeLeadingSlash, 3);
29
30
                if ($splitNamespace[0] === $moduleNameSpaceStart) {
31
                    continue;
32
                }
33
34
                $namespacesFromFiles[] = $splitNamespace[0] . '\\' . $splitNamespace[1] . '\\';
35
            }
36
        }
37
38
        return array_unique($namespacesFromFiles);
39
    }
40
}
41