ScriptLoader::_getLocation()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 8
nc 4
nop 1
1
<?php
2
3
namespace N98\Magento\Command\Script\Repository;
4
5
use N98\Util\OperatingSystem;
6
use Symfony\Component\Finder\Finder;
7
use Symfony\Component\Finder\SplFileInfo;
8
9
class ScriptLoader
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $_scriptFiles = array();
15
16
    /**
17
     * @var string
18
     */
19
    protected $_homeScriptFolder = '';
20
21
    /**
22
     * @var string
23
     */
24
    protected $_magentoRootFolder = '';
25
26
    /**
27
     * @var array
28
     */
29
    protected $_scriptFolders = array();
30
31
    /**
32
     * @param array  $scriptFolders
33
     * @param string $magentoRootFolder
1 ignored issue
show
Documentation introduced by
Should the type for parameter $magentoRootFolder not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
34
     */
35
    public function __construct(array $scriptFolders, $magentoRootFolder = null)
36
    {
37
        $this->_magentoRootFolder = $magentoRootFolder;
38
        if (OperatingSystem::isWindows()) {
39
            $this->_homeScriptFolder = OperatingSystem::getHomeDir() . '/n98-magerun2/scripts';
40
        } else {
41
            $this->_homeScriptFolder = OperatingSystem::getHomeDir() . '/.n98-magerun2/scripts';
42
        }
43
44
        $this->_scriptFolders = $scriptFolders;
45
        $this->_scriptFolders[] = $this->_homeScriptFolder;
46
        foreach ($this->_scriptFolders as $key => $scriptFolder) {
47
            if (!is_dir($scriptFolder)) {
48
                unset($this->_scriptFolders[$key]);
49
            }
50
        }
51
52
        if (count($this->_scriptFolders)) {
53
            $this->findScripts();
54
        }
55
    }
56
57
    protected function findScripts()
58
    {
59
        $finder = Finder::create()
60
            ->files()
61
            ->followLinks(true)
0 ignored issues
show
Unused Code introduced by
The call to Finder::followLinks() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
62
            ->ignoreUnreadableDirs(true)
63
            ->name('*.magerun')
64
            ->in($this->_scriptFolders);
65
66
        $this->_scriptFiles = array();
67
        foreach ($finder as $file) { /* @var $file SplFileInfo */
68
            $this->_scriptFiles[$file->getFilename()] = array(
69
                'fileinfo'    => $file,
70
                'description' => $this->_readFirstLineOfFile($file->getPathname()),
71
                'location'    => $this->_getLocation($file->getPathname()),
72
            );
73
        }
74
75
        ksort($this->_scriptFiles);
76
    }
77
78
    /**
79
     * Reads the first line. If it's a comment return it.
80
     *
81
     * @param string $file
82
     *
83
     * @return string
84
     */
85
    protected function _readFirstLineOfFile($file)
86
    {
87
        $f = @fopen($file, 'r');
88
        if (!$f) {
89
            return '';
90
        }
91
        $line = trim(fgets($f));
92
        fclose($f);
93
94
        if (isset($line[0]) && $line[0] != '#') {
95
            return '';
96
        }
97
98
        return trim(substr($line, 1));
99
    }
100
101
    /**
102
     * @param string $pathname
103
     *
104
     * @return string
105
     */
106
    protected function _getLocation($pathname)
107
    {
108
        if (strstr($pathname, $this->_magentoRootFolder)) {
109
            return 'project';
110
        }
111
112
        if (dirname($pathname) == $this->_homeScriptFolder) {
113
            return 'personal';
114
        }
115
116
        if (strstr($pathname, 'n98-magerun/modules')) {
117
            return 'module';
118
        }
119
120
        return 'system';
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    public function getFiles()
127
    {
128
        return $this->_scriptFiles;
129
    }
130
}
131