SpiderLinuxCommand   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 207
Duplicated Lines 8.7 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 18
loc 207
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 3

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 2
A getAdapter() 0 5 1
A getFilesystem() 0 5 1
A run() 7 29 3
A trigger() 0 6 2
A getIgnoredPaths() 0 4 1
A getAnalysedPaths() 0 4 1
A setResultsFilter() 0 4 1
A setAnalysedPaths() 0 4 1
A getAnalysisToolsClasses() 0 4 1
A getAnalysisTools() 0 12 2
A createResult() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Finder\Spider;
4
5
use Symfony\Component\Finder\Finder as FinderBase;
6
7
use Finder\Logic\Output\AbstractOutput;
8
use Finder\Logic\Output\Filter\OutputFilterInterface;
9
use Finder\Logic\Output\TriggerableInterface;
10
11
use League\Flysystem\Adapter\Local;
12
use League\Flysystem\Filesystem;
13
14
/**
15
 * Run all script analysers and outputs their result.
16
 *
17
 * @package qa
18
 */
19
class SpiderLinuxCommand
20
{
21
    const EVENT_STARTING_ANALYSIS = 0;
22
    const EVENT_STARTING_TOOL = 1;
23
    const EVENT_FINISHED_TOOL = 2;
24
    const EVENT_FINISHED_ANALYSIS = 3;
25
26
    const VERSION = '0.7.1';
27
28
    protected $defaultLanguage = 'Php';
29
30
    /**
31
     * Composer binaries path.
32
     *
33
     * @var string directory path.
34
     */
35
    protected $binariesPath;
36
37
    /**
38
     * Analysis target.
39
     *
40
     * @var string[] file or directory path.
41
     */
42
    protected $analysedPaths;
43
44
    /**
45
     * Ignored paths.
46
     *
47
     * @var string[] comma separated list of directories to ignore.
48
     */
49
    protected $ignoredPaths;
50
51
    /**
52
     * Output service.
53
     *
54
     * @var AbstractOutput output instance.
55
     */
56
    protected $output;
57
58
    /**
59
     * Analysis result filter.
60
     *
61
     * @var OutputFilterInterface filter instance.
62
     */
63
    protected $resultsFilter;
64
65
    /**
66
     * Set dependencies and initialize CLI.
67
     *
68
     * @param AbstractOutput $output        Output target.
69
     * @param string         $binariesPath  Composer binaries path.
70
     * @param string[]       $analysedPaths target file or directory path.
71
     * @param string[]       $ignoredPaths  comma separated list of ignored directories.
72
     */
73 View Code Duplication
    public function __construct(AbstractOutput $output, $binariesPath, $analysedPaths, $ignoredPaths, $project = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        $this->output = $output;
76
        $this->binariesPath = $binariesPath;
77
        $this->analysedPaths = $analysedPaths;
78
        $this->ignoredPaths = $ignoredPaths;
79
80
        if (!$project) {
81
            $this->languageClass = 'Finder\\Actions\\Worker\\Analyser\\Language\\'.$this->defaultLanguage;
0 ignored issues
show
Bug introduced by
The property languageClass does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
82
        }
83
    }
84
85
    public function getAdapter()
86
    {
87
88
        $adapter = new Local(__DIR__.'/path/to/root/');
0 ignored issues
show
Unused Code introduced by
$adapter is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
    }
90
91
    public function getFilesystem()
92
    {
93
94
        $filesystem = new Filesystem($adapter);
0 ignored issues
show
Bug introduced by
The variable $adapter does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Unused Code introduced by
$filesystem is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
95
    }
96
97
    /**
98
     * Run each configured PHP analysis tool.
99
     *
100
     * @return boolean true if it didn't find code issues.
101
     */
102
    public function run()
103
    {
104
        $result = $this->createResult();
105
        $this->trigger(
106
            self::EVENT_STARTING_ANALYSIS,
107
            ['ignoredPaths' => $this->ignoredPaths]
0 ignored issues
show
Documentation introduced by
array('ignoredPaths' => $this->ignoredPaths) is of type array<string,array<integ...rray<integer,string>"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
        );
109
110
        $this->runTargetDetectAndCollect();
0 ignored issues
show
Bug introduced by
The method runTargetDetectAndCollect() does not seem to exist on object<Finder\Spider\SpiderLinuxCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
        $this->runTargetMetrics();
0 ignored issues
show
Bug introduced by
The method runTargetMetrics() does not seem to exist on object<Finder\Spider\SpiderLinuxCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
        $this->runTargetAnalysis();
0 ignored issues
show
Bug introduced by
The method runTargetAnalysis() does not seem to exist on object<Finder\Spider\SpiderLinuxCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
114 View Code Duplication
        foreach ($this->getAnalysisTools() as $tool) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
            $message = ['description' => $tool->getDescription()];
116
            $this->trigger(self::EVENT_STARTING_TOOL, $message);
0 ignored issues
show
Documentation introduced by
$message is of type array<string,?,{"description":"?"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
            $tool->run($this->getAnalysedPaths());
118
            $result->mergeWith($tool->getAnalysisResult());
119
            $this->trigger(self::EVENT_FINISHED_TOOL);
120
        }
121
122
        if ($this->resultsFilter) {
123
            $result->setResultsFilter($this->resultsFilter);
124
        }
125
126
        $this->output->result($result);
127
        $this->trigger(self::EVENT_FINISHED_ANALYSIS);
128
129
        return !$result->hasIssues();
130
    }
131
132
    /**
133
     * Call an output trigger if supported.
134
     *
135
     * @param  int         $event   occurred event.
136
     * @param  string|null $message optional message.
137
     * @return void
138
     */
139
    protected function trigger($event, $message = null)
140
    {
141
        if ($this->output instanceof TriggerableInterface) {
142
            $this->output->trigger($event, $message);
143
        }
144
    }
145
146
    /**
147
     * Get a list of paths to be ignored by the analysis.
148
     *
149
     * @return string[] a list of file and/or directory paths.
150
     */
151
    public function getIgnoredPaths()
152
    {
153
        return $this->ignoredPaths;
154
    }
155
156
    /**
157
     * Analysis target path.
158
     *
159
     * @return string[] target path.
160
     */
161
    public function getAnalysedPaths()
162
    {
163
        return $this->analysedPaths;
164
    }
165
166
    /**
167
     * Add an output filter to delegate to the analysis result object.
168
     *
169
     * @param OutputFilterInterface $filter filter instance.
170
     */
171
    public function setResultsFilter(OutputFilterInterface $filter)
172
    {
173
        $this->resultsFilter = $filter;
174
    }
175
176
    /**
177
     * Set target files and/or directories to be analysed.
178
     *
179
     * @param  string[] $paths target paths.
180
     * @return void
181
     */
182
    public function setAnalysedPaths(array $paths)
183
    {
184
        $this->analysedPaths = $paths;
185
    }
186
187
    /**
188
     * List of PHP analys integration classes.
189
     *
190
     * @return string[] array of class names.
191
     */
192
    protected function getAnalysisToolsClasses()
193
    {
194
        $this->languageClass::getAnalysisToolsClasses();
195
    }
196
197
    /**
198
     * Set of PHP analys integration objects.
199
     *
200
     * @return Finder\Logic\Tools\AbstractIntegrationTool[] set of objects.
201
     */
202
    protected function getAnalysisTools()
203
    {
204
        $objects = [];
205
206
        foreach ($this->getAnalysisToolsClasses() as $className) {
0 ignored issues
show
Bug introduced by
The expression $this->getAnalysisToolsClasses() of type array<integer,string>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
207
            $tool = new $className($this->binariesPath, sys_get_temp_dir());
208
            $tool->setIgnoredPaths($this->getIgnoredPaths());
209
            $objects[] = $tool;
210
        }
211
212
        return $objects;
213
    }
214
215
    /**
216
     * Create an empty analysis result.
217
     *
218
     * @return AnalysisResult instance.
219
     */
220
    protected function createResult()
221
    {
222
        return new AnalysisResult;
223
    }
224
225
}
226