-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathLint.php
More file actions
139 lines (115 loc) · 3.24 KB
/
Copy pathLint.php
File metadata and controls
139 lines (115 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace PHPCensor\Plugin;
use DirectoryIterator;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
/**
* PHP Lint Plugin - Provides access to PHP lint functionality.
*
* @package PHP Censor
* @subpackage Application
*
* @author Dan Cryer <dan@block8.co.uk>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class Lint extends Plugin
{
protected $directories;
protected $recursive = true;
/**
* @return string
*/
public static function pluginName()
{
return 'lint';
}
/**
* {@inheritDoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
$this->directories = [
$this->directory,
];
if (!empty($options['directories']) && \is_array($options['directories'])) {
foreach ($options['directories'] as $index => $directory) {
$relativePath = \preg_replace(
'#^(\./|/)?(.*)$#',
'$2',
$options['directories'][$index]
);
$relativePath = \rtrim($relativePath, "\//");
$this->directories[] = $this->builder->buildPath . $relativePath . '/';
}
}
if (\array_key_exists('recursive', $options)) {
$this->recursive = $options['recursive'];
}
}
/**
* Executes parallel lint
*/
public function execute()
{
$success = true;
foreach ($this->directories as $dir) {
if (!$this->lintDirectory($dir)) {
$success = false;
}
}
return $success;
}
/**
* Lint an item (file or directory) by calling the appropriate method.
* @return bool
*/
protected function lintItem($item, $itemPath)
{
$success = true;
if ($item->isFile() && $item->getExtension() === 'php' && !$this->lintFile($itemPath)) {
$success = false;
} elseif ($item->isDir() &&
$this->recursive &&
!$this->lintDirectory($itemPath . '/')) {
$success = false;
}
return $success;
}
/**
* Run php -l against a directory of files.
* @return bool
*/
protected function lintDirectory($path)
{
$success = true;
$directory = new DirectoryIterator($this->builder->buildPath . $path);
foreach ($directory as $item) {
if ($item->isDot()) {
continue;
}
$itemPath = $path . $item->getFilename();
if (\in_array($itemPath, $this->ignore, true)) {
continue;
}
if (!$this->lintItem($item, $itemPath)) {
$success = false;
}
}
return $success;
}
/**
* Run php -l against a specific file.
* @return bool
*/
protected function lintFile($path)
{
$success = true;
if (!$this->builder->executeCommand(Builder::PHP_CLI_TAG . ' -l "%s"', $this->builder->buildPath . $path)) {
$this->builder->logFailure($path);
$success = false;
}
return $success;
}
}