Completed
Push — develop ( 590167...59c352 )
by Tom
03:40
created

ScriptLoader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
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 string
13
     */
14
    private $homeDir;
15
16
    /**
17
     * @var array
18
     */
19
    protected $_scriptFiles = array();
20
21
    /**
22
     * @var string
23
     * @deprecated since 1.97.29
24
     */
25
    protected $_homeScriptFolder = '';
26
27
    /**
28
     * @var string
29
     */
30
    protected $_magentoRootFolder = '';
31
32
    /**
33
     * @var array
34
     */
35
    protected $_scriptFolders = array();
36
37
    /**
38
     * @param array  $scriptFolders
39
     * @param string $magentoRootFolder
40
     */
41
    public function __construct(array $scriptFolders, $magentoRootFolder = null)
42
    {
43
        $this->homeDir = OperatingSystem::getHomeDir();
0 ignored issues
show
Documentation Bug introduced by
It seems like \N98\Util\OperatingSystem::getHomeDir() can also be of type false. However, the property $homeDir is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
44
45
        $this->_magentoRootFolder = $magentoRootFolder;
46
47
        if (OperatingSystem::isWindows()) {
48
            $scriptFolders[] = $this->homeDir . '/n98-magerun/scripts';
49
        }
50
        $scriptFolders[] = $this->homeDir . '/.n98-magerun/scripts';
51
52
        $this->findScripts($scriptFolders);
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    public function getFiles()
59
    {
60
        return $this->_scriptFiles;
61
    }
62
63
    protected function findScripts(array $scriptFolders = null)
64
    {
65
        if (null === $scriptFolders) {
66
            $scriptFolders = $this->_scriptFolders;
67
        }
68
69
        $scriptFolders = array_filter(array_filter($scriptFolders, 'strlen'), 'is_dir');
70
71
        $this->_scriptFolders = $scriptFolders;
72
        $this->_scriptFiles = array();
73
        if (1 > count($scriptFolders)) {
74
            return;
75
        }
76
77
        $finder = Finder::create()
78
            ->files()
79
            ->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...
80
            ->ignoreUnreadableDirs(true)
81
            ->name('*.magerun')
82
            ->in($scriptFolders);
83
84
        $scriptFiles = array();
85
        foreach ($finder as $file) { /* @var $file SplFileInfo */
86
            $scriptFiles[$file->getFilename()] = array(
87
                'fileinfo'    => $file,
88
                'description' => $this->_readFirstLineOfFile($file->getPathname()),
89
                'location'    => $this->_getLocation($file->getPathname()),
90
            );
91
        }
92
93
        ksort($scriptFiles);
94
        $this->_scriptFiles = $scriptFiles;
95
    }
96
97
    /**
98
     * Reads the first line. If it's a comment return it.
99
     *
100
     * @param string $file
101
     *
102
     * @return string
103
     */
104
    protected function _readFirstLineOfFile($file)
105
    {
106
        $f = @fopen($file, 'r');
107
        if (!$f) {
108
            return '';
109
        }
110
        $line = trim(fgets($f));
111
        fclose($f);
112
113
        if (isset($line[0]) && $line[0] != '#') {
114
            return '';
115
        }
116
117
        return trim(substr($line, 1));
118
    }
119
120
    /**
121
     * @param string $pathname
122
     *
123
     * @return string
124
     */
125
    protected function _getLocation($pathname)
126
    {
127
        if (strstr($pathname, $this->_magentoRootFolder)) {
128
            return 'project';
129
        }
130
131
        if (strstr($pathname, $this->homeDir)) {
132
            return 'personal';
133
        }
134
135
        if (strstr($pathname, 'n98-magerun/modules')) {
136
            return 'module';
137
        }
138
139
        return 'system';
140
    }
141
}
142