Skip to content

Commit 4929361

Browse files
committed
Feature: show deprecated php warnings (#94)
1 parent 2971dd8 commit 4929361

File tree

8 files changed

+103
-12
lines changed

8 files changed

+103
-12
lines changed

phpcs-ruleset.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
<rule ref="Generic.Files.LineLength">
4141
<properties>
42-
<property name="lineLimit" value="125"/>
42+
<property name="lineLimit" value="135"/>
4343
<property name="absoluteLineLimit" value="200"/>
4444
</properties>
4545
</rule>

src/Manager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function run(Settings $settings = null)
6565
$parallelLint = new ParallelLint($phpExecutable, $settings->parallelJobs);
6666
$parallelLint->setAspTagsEnabled($settings->aspTags);
6767
$parallelLint->setShortTagEnabled($settings->shortTag);
68+
$parallelLint->setShowDeprecated($settings->showDeprecated);
6869

6970
$parallelLint->setProcessCallback(function ($status, $file) use ($output) {
7071
if ($status === ParallelLint::STATUS_OK) {

src/ParallelLint.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class ParallelLint
5656
/** @var callable */
5757
private $processCallback;
5858

59+
/** @var bool */
60+
private $showDeprecated = false;
61+
5962
public function __construct(PhpExecutable $phpExecutable, $parallelJobs = 10)
6063
{
6164
$this->phpExecutable = $phpExecutable;
@@ -95,7 +98,8 @@ public function lint(array $files)
9598
$this->phpExecutable,
9699
$file,
97100
$this->aspTagsEnabled,
98-
$this->shortTagEnabled
101+
$this->shortTagEnabled,
102+
$this->showDeprecated
99103
);
100104
}
101105
}
@@ -115,14 +119,15 @@ public function lint(array $files)
115119
$skippedFiles[] = $file;
116120
$processCallback(self::STATUS_SKIP, $file);
117121

122+
} else if ($process->containsError()) {
123+
$checkedFiles[] = $file;
124+
$errors[] = new SyntaxError($file, $process->getSyntaxError());
125+
$processCallback(self::STATUS_ERROR, $file);
126+
118127
} else if ($process->isSuccess()) {
119128
$checkedFiles[] = $file;
120129
$processCallback(self::STATUS_OK, $file);
121130

122-
} else if ($process->hasSyntaxError()) {
123-
$checkedFiles[] = $file;
124-
$errors[] = new SyntaxError($file, $process->getSyntaxError());
125-
$processCallback(self::STATUS_ERROR, $file);
126131

127132
} else {
128133
$errors[] = new Error($file, $process->getOutput());
@@ -153,7 +158,7 @@ public function lint(array $files)
153158
$checkedFiles[] = $file;
154159
$processCallback(self::STATUS_OK, $file);
155160

156-
} else if ($process->hasSyntaxError()) {
161+
} else if ($process->containsError()) {
157162
$checkedFiles[] = $file;
158163
$errors[] = new SyntaxError($file, $process->getSyntaxError());
159164
$processCallback(self::STATUS_ERROR, $file);
@@ -264,4 +269,23 @@ public function setShortTagEnabled($shortTagEnabled)
264269

265270
return $this;
266271
}
272+
273+
/**
274+
* @return boolean
275+
*/
276+
public function isShowDeprecated()
277+
{
278+
return $this->showDeprecated;
279+
}
280+
281+
/**
282+
* @param $showDeprecated
283+
* @return ParallelLint
284+
*/
285+
public function setShowDeprecated($showDeprecated)
286+
{
287+
$this->showDeprecated = $showDeprecated;
288+
289+
return $this;
290+
}
267291
}

src/Process/LintProcess.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@ class LintProcess extends PhpProcess
77
{
88
const FATAL_ERROR = 'Fatal error';
99
const PARSE_ERROR = 'Parse error';
10+
const DEPRECATED_ERROR = 'Deprecated:';
11+
12+
/**
13+
* @var bool
14+
*/
15+
private $showDeprecatedErrors;
1016

1117
/**
1218
* @param PhpExecutable $phpExecutable
1319
* @param string $fileToCheck Path to file to check
1420
* @param bool $aspTags
1521
* @param bool $shortTag
22+
* @param bool $deprecated
1623
*/
17-
public function __construct(PhpExecutable $phpExecutable, $fileToCheck, $aspTags = false, $shortTag = false)
24+
public function __construct(PhpExecutable $phpExecutable, $fileToCheck, $aspTags = false, $shortTag = false, $deprecated = false)
1825
{
1926
if (empty($fileToCheck)) {
2027
throw new \InvalidArgumentException("File to check must be set.");
@@ -29,13 +36,14 @@ public function __construct(PhpExecutable $phpExecutable, $fileToCheck, $aspTags
2936
escapeshellarg($fileToCheck),
3037
);
3138

39+
$this->showDeprecatedErrors = $deprecated;
3240
parent::__construct($phpExecutable, $parameters);
3341
}
3442

3543
/**
3644
* @return bool
3745
*/
38-
public function hasSyntaxError()
46+
public function containsError()
3947
{
4048
return $this->containsParserOrFatalError($this->getOutput());
4149
}
@@ -46,8 +54,7 @@ public function hasSyntaxError()
4654
*/
4755
public function getSyntaxError()
4856
{
49-
if ($this->hasSyntaxError()) {
50-
// Look for fatal errors first
57+
if ($this->containsError()) {
5158
foreach (explode("\n", $this->getOutput()) as $line) {
5259
if ($this->containsFatalError($line)) {
5360
return $line;
@@ -107,6 +114,17 @@ private function containsParserError($string)
107114
*/
108115
private function containsFatalError($string)
109116
{
110-
return strpos($string, self::FATAL_ERROR) !== false;
117+
return strpos($string, self::FATAL_ERROR) !== false ||
118+
strpos($string, self::PARSE_ERROR) !== false ||
119+
$this->containsDeprecatedError($string);
120+
}
121+
122+
private function containsDeprecatedError($string)
123+
{
124+
if ($this->showDeprecatedErrors === false) {
125+
return false;
126+
}
127+
128+
return strpos($string, self::DEPRECATED_ERROR) !== false;
111129
}
112130
}

src/Settings.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ class Settings
127127
*/
128128
public $ignoreFails = false;
129129

130+
/**
131+
* @var bool
132+
*/
133+
public $showDeprecated = false;
134+
130135
/**
131136
* @param array $paths
132137
*/
@@ -217,6 +222,10 @@ public static function parseArguments(array $arguments)
217222
$settings->ignoreFails = true;
218223
break;
219224

225+
case '--show-deprecated':
226+
$settings->showDeprecated = true;
227+
break;
228+
220229
default:
221230
throw new InvalidArgumentException($argument);
222231
}

tests/ParallelLint.lint.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,28 @@ class ParallelLintLintTest extends Tester\TestCase
9292
Assert::equal(1, count($result->getErrors()));
9393
}
9494

95+
public function testDeprecated()
96+
{
97+
$parallelLint = new ParallelLint($this->getPhpExecutable());
98+
$result = $parallelLint->lint(array(__DIR__ . '/examples/example-05/Foo.php'));
99+
Assert::equal(1, $result->getCheckedFilesCount());
100+
Assert::equal(0, $result->getFilesWithSyntaxErrorCount());
101+
Assert::false($result->hasSyntaxError());
102+
Assert::equal(0, count($result->getErrors()));
103+
104+
if (PHP_VERSION_ID < 70000) {
105+
Tester\Environment::skip('test for php version > 7.0');
106+
}
107+
108+
$parallelLint = new ParallelLint($this->getPhpExecutable());
109+
$parallelLint->setShowDeprecated(true);
110+
$result = $parallelLint->lint(array(__DIR__ . '/examples/example-05/Foo.php'));
111+
Assert::equal(1, $result->getCheckedFilesCount());
112+
Assert::equal(1, $result->getFilesWithSyntaxErrorCount());
113+
Assert::true($result->hasSyntaxError());
114+
Assert::equal(1, count($result->getErrors()));
115+
}
116+
95117
public function testValidAndInvalidFiles()
96118
{
97119
$parallelLint = new ParallelLint($this->getPhpExecutable());

tests/Settings.parseArguments.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class SettingsParseArgumentsTest extends Tester\TestCase
5858
$expectedSettings->colors = Settings::DISABLED;
5959
$expectedSettings->showProgress = true;
6060
$expectedSettings->format = Settings::FORMAT_TEXT;
61+
$expectedSettings->deprecated = false;
6162

6263
Assert::equal($expectedSettings->phpExecutable, $settings->phpExecutable);
6364
Assert::equal($expectedSettings->shortTag, $settings->shortTag);
@@ -69,6 +70,7 @@ class SettingsParseArgumentsTest extends Tester\TestCase
6970
Assert::equal($expectedSettings->colors, $settings->colors);
7071
Assert::equal($expectedSettings->showProgress, $settings->showProgress);
7172
Assert::equal($expectedSettings->format, $settings->format);
73+
Assert::equal($expectedSettings->showDeprecated, $settings->showDeprecated);
7274
}
7375

7476
public function testColorsForced()

tests/examples/example-05/Foo.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
class Foo
4+
{
5+
6+
/**
7+
* @var string
8+
*/
9+
private $bar;
10+
11+
public function Foo($bar)
12+
{
13+
$this->bar = $bar;
14+
}
15+
}

0 commit comments

Comments
 (0)