|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fabrica\Tools\Plugin; |
|
4
|
|
|
|
|
5
|
|
|
use Fabrica\Tools; |
|
6
|
|
|
use Fabrica\Tools\Builder; |
|
7
|
|
|
use Fabrica\Models\Infra\Ci\Build; |
|
8
|
|
|
use Fabrica\Tools\Plugin; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* PHP Lint Plugin - Provides access to PHP lint functionality. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Ricardo Sierra <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class Lint extends Plugin |
|
16
|
|
|
{ |
|
17
|
|
|
protected $directories; |
|
18
|
|
|
protected $recursive = true; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return string |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function pluginName() |
|
24
|
|
|
{ |
|
25
|
|
|
return 'lint'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Builder $builder, Build $build, array $options = []) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct($builder, $build, $options); |
|
34
|
|
|
|
|
35
|
|
|
$this->directories = [ |
|
36
|
|
|
$this->directory, |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
if (!empty($options['directories']) && is_array($options['directories'])) { |
|
40
|
|
|
foreach ($options['directories'] as $index => $directory) { |
|
41
|
|
|
$relativePath = preg_replace( |
|
42
|
|
|
'#^(\./|/)?(.*)$#', |
|
43
|
|
|
'$2', |
|
44
|
|
|
$options['directories'][$index] |
|
45
|
|
|
); |
|
46
|
|
|
$relativePath = rtrim($relativePath, "\//"); |
|
47
|
|
|
|
|
48
|
|
|
$this->directories[] = $this->builder->buildPath . $relativePath . '/'; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (array_key_exists('recursive', $options)) { |
|
53
|
|
|
$this->recursive = $options['recursive']; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Executes parallel lint |
|
59
|
|
|
*/ |
|
60
|
|
|
public function execute() |
|
61
|
|
|
{ |
|
62
|
|
|
$success = true; |
|
63
|
|
|
|
|
64
|
|
|
$php = $this->findBinary('php'); |
|
65
|
|
|
|
|
66
|
|
|
foreach ($this->directories as $dir) { |
|
67
|
|
|
if (!$this->lintDirectory($php, $dir)) { |
|
68
|
|
|
$success = false; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $success; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Lint an item (file or directory) by calling the appropriate method. |
|
77
|
|
|
* |
|
78
|
|
|
* @param $php |
|
79
|
|
|
* @param $item |
|
80
|
|
|
* @param $itemPath |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function lintItem($php, $item, $itemPath) |
|
84
|
|
|
{ |
|
85
|
|
|
$success = true; |
|
86
|
|
|
|
|
87
|
|
|
if ($item->isFile() && $item->getExtension() == 'php' && !$this->lintFile($php, $itemPath)) { |
|
88
|
|
|
$success = false; |
|
89
|
|
View Code Duplication |
} elseif ($item->isDir() |
|
|
|
|
|
|
90
|
|
|
&& $this->recursive |
|
91
|
|
|
&& !$this->lintDirectory($php, ($itemPath . '/')) |
|
92
|
|
|
) { |
|
93
|
|
|
$success = false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $success; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Run php -l against a directory of files. |
|
101
|
|
|
* |
|
102
|
|
|
* @param $php |
|
103
|
|
|
* @param $path |
|
104
|
|
|
* @return bool |
|
105
|
|
|
*/ |
|
106
|
|
View Code Duplication |
protected function lintDirectory($php, $path) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
$success = true; |
|
109
|
|
|
$directory = new \DirectoryIterator($this->builder->buildPath . $path); |
|
110
|
|
|
|
|
111
|
|
|
foreach ($directory as $item) { |
|
112
|
|
|
if ($item->isDot()) { |
|
113
|
|
|
continue; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$itemPath = $path . $item->getFilename(); |
|
117
|
|
|
|
|
118
|
|
|
if (in_array($itemPath, $this->ignore)) { |
|
119
|
|
|
continue; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if (!$this->lintItem($php, $item, $itemPath)) { |
|
123
|
|
|
$success = false; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $success; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Run php -l against a specific file. |
|
132
|
|
|
* |
|
133
|
|
|
* @param $php |
|
134
|
|
|
* @param $path |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function lintFile($php, $path) |
|
138
|
|
|
{ |
|
139
|
|
|
$success = true; |
|
140
|
|
|
|
|
141
|
|
|
if (!$this->builder->executeCommand($php . ' -l "%s"', $this->builder->buildPath . $path)) { |
|
142
|
|
|
$this->builder->logFailure($path); |
|
143
|
|
|
$success = false; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $success; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
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.