Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Feature: show deprecated php warnings #94

Merged
merged 4 commits into from
Sep 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpcs-ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="125"/>
<property name="lineLimit" value="135"/>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather use multi-line constructor. This rule is quite important

Copy link
Contributor Author

@mzk mzk Mar 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public function __construct(
        PhpExecutable $phpExecutable,
        $fileToCheck,
        $aspTags = false,
        $shortTag = false,
        $deprecated = false)
    {
FILE: /private/var/www/PHP-Parallel-Lint/src/Process/LintProcess.php
----------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
----------------------------------------------------------------------
 29 | ERROR | [x] The closing parenthesis of a multi-line function
    |       |     declaration must be on a new line
    |       |     (PEAR.Functions.FunctionDeclaration.CloseBracketLine)
 30 | ERROR | [x] The closing parenthesis and the opening brace of a
    |       |     multi-line function declaration must be on the same
    |       |     line
    |       |     (PEAR.Functions.FunctionDeclaration.NewlineBeforeOpenBrace)
    public function __construct(
        PhpExecutable $phpExecutable,
        $fileToCheck,
        $aspTags = false,
        $shortTag = false,
        $deprecated = false
    )
    {
 31 | ERROR | [x] The closing parenthesis and the opening brace of a
    |       |     multi-line function declaration must be on the same
    |       |     line
    |       |     (PEAR.Functions.FunctionDeclaration.NewlineBeforeOpenBrace)
    public function __construct(
        PhpExecutable $phpExecutable,
        $fileToCheck,
        $aspTags = false,
        $shortTag = false,
        $deprecated = false
    ) {
Opening brace should be on a new line
    |       |     (Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine)

So, isn't possible with this ruleset

Copy link

@TomasVotruba TomasVotruba Mar 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Crazy :D

I think I've disabled one of those rules for these reasons.

<property name="absoluteLineLimit" value="200"/>
</properties>
</rule>
Expand Down
1 change: 1 addition & 0 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function run(Settings $settings = null)
$parallelLint = new ParallelLint($phpExecutable, $settings->parallelJobs);
$parallelLint->setAspTagsEnabled($settings->aspTags);
$parallelLint->setShortTagEnabled($settings->shortTag);
$parallelLint->setShowDeprecated($settings->showDeprecated);

$parallelLint->setProcessCallback(function ($status, $file) use ($output) {
if ($status === ParallelLint::STATUS_OK) {
Expand Down
36 changes: 30 additions & 6 deletions src/ParallelLint.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class ParallelLint
/** @var callable */
private $processCallback;

/** @var bool */
private $showDeprecated = false;

public function __construct(PhpExecutable $phpExecutable, $parallelJobs = 10)
{
$this->phpExecutable = $phpExecutable;
Expand Down Expand Up @@ -95,7 +98,8 @@ public function lint(array $files)
$this->phpExecutable,
$file,
$this->aspTagsEnabled,
$this->shortTagEnabled
$this->shortTagEnabled,
$this->showDeprecated
);
}
}
Expand All @@ -115,14 +119,15 @@ public function lint(array $files)
$skippedFiles[] = $file;
$processCallback(self::STATUS_SKIP, $file);

} else if ($process->containsError()) {
$checkedFiles[] = $file;
$errors[] = new SyntaxError($file, $process->getSyntaxError());
$processCallback(self::STATUS_ERROR, $file);

} else if ($process->isSuccess()) {
$checkedFiles[] = $file;
$processCallback(self::STATUS_OK, $file);

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

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

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

return $this;
}

/**
* @return boolean
*/
public function isShowDeprecated()
{
return $this->showDeprecated;
}

/**
* @param $showDeprecated
* @return ParallelLint
*/
public function setShowDeprecated($showDeprecated)
{
$this->showDeprecated = $showDeprecated;

return $this;
}
}
28 changes: 23 additions & 5 deletions src/Process/LintProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ class LintProcess extends PhpProcess
{
const FATAL_ERROR = 'Fatal error';
const PARSE_ERROR = 'Parse error';
const DEPRECATED_ERROR = 'Deprecated:';

/**
* @var bool
*/
private $showDeprecatedErrors;

/**
* @param PhpExecutable $phpExecutable
* @param string $fileToCheck Path to file to check
* @param bool $aspTags
* @param bool $shortTag
* @param bool $deprecated
*/
public function __construct(PhpExecutable $phpExecutable, $fileToCheck, $aspTags = false, $shortTag = false)
public function __construct(PhpExecutable $phpExecutable, $fileToCheck, $aspTags = false, $shortTag = false, $deprecated = false)
{
if (empty($fileToCheck)) {
throw new \InvalidArgumentException("File to check must be set.");
Expand All @@ -29,13 +36,14 @@ public function __construct(PhpExecutable $phpExecutable, $fileToCheck, $aspTags
escapeshellarg($fileToCheck),
);

$this->showDeprecatedErrors = $deprecated;
parent::__construct($phpExecutable, $parameters);
}

/**
* @return bool
*/
public function hasSyntaxError()
public function containsError()
{
return $this->containsParserOrFatalError($this->getOutput());
}
Expand All @@ -46,8 +54,7 @@ public function hasSyntaxError()
*/
public function getSyntaxError()
{
if ($this->hasSyntaxError()) {
// Look for fatal errors first
if ($this->containsError()) {
foreach (explode("\n", $this->getOutput()) as $line) {
if ($this->containsFatalError($line)) {
return $line;
Expand Down Expand Up @@ -107,6 +114,17 @@ private function containsParserError($string)
*/
private function containsFatalError($string)
{
return strpos($string, self::FATAL_ERROR) !== false;
return strpos($string, self::FATAL_ERROR) !== false ||
strpos($string, self::PARSE_ERROR) !== false ||
$this->containsDeprecatedError($string);
}

private function containsDeprecatedError($string)
{
if ($this->showDeprecatedErrors === false) {
return false;
}

return strpos($string, self::DEPRECATED_ERROR) !== false;
}
}
9 changes: 9 additions & 0 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ class Settings
*/
public $ignoreFails = false;

/**
* @var bool
*/
public $showDeprecated = false;

/**
* @param array $paths
*/
Expand Down Expand Up @@ -212,6 +217,10 @@ public static function parseArguments(array $arguments)
$settings->ignoreFails = true;
break;

case '--show-deprecated':
$settings->showDeprecated = true;
break;

default:
throw new InvalidArgumentException($argument);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/ParallelLint.lint.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ class ParallelLintLintTest extends Tester\TestCase
Assert::equal(1, count($result->getErrors()));
}

public function testDeprecated()
{
$parallelLint = new ParallelLint($this->getPhpExecutable());
$result = $parallelLint->lint(array(__DIR__ . '/examples/example-05/Foo.php'));
Assert::equal(1, $result->getCheckedFilesCount());
Assert::equal(0, $result->getFilesWithSyntaxErrorCount());
Assert::false($result->hasSyntaxError());
Assert::equal(0, count($result->getErrors()));

if (PHP_VERSION_ID < 70000) {
Tester\Environment::skip('test for php version > 7.0');
}

$parallelLint = new ParallelLint($this->getPhpExecutable());
$parallelLint->setShowDeprecated(true);
$result = $parallelLint->lint(array(__DIR__ . '/examples/example-05/Foo.php'));
Assert::equal(1, $result->getCheckedFilesCount());
Assert::equal(1, $result->getFilesWithSyntaxErrorCount());
Assert::true($result->hasSyntaxError());
Assert::equal(1, count($result->getErrors()));
}

public function testValidAndInvalidFiles()
{
$parallelLint = new ParallelLint($this->getPhpExecutable());
Expand Down
2 changes: 2 additions & 0 deletions tests/Settings.parseArguments.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class SettingsParseArgumentsTest extends Tester\TestCase
$expectedSettings->colors = Settings::DISABLED;
$expectedSettings->showProgress = true;
$expectedSettings->format = Settings::FORMAT_TEXT;
$expectedSettings->deprecated = false;

Assert::equal($expectedSettings->phpExecutable, $settings->phpExecutable);
Assert::equal($expectedSettings->shortTag, $settings->shortTag);
Expand All @@ -69,6 +70,7 @@ class SettingsParseArgumentsTest extends Tester\TestCase
Assert::equal($expectedSettings->colors, $settings->colors);
Assert::equal($expectedSettings->showProgress, $settings->showProgress);
Assert::equal($expectedSettings->format, $settings->format);
Assert::equal($expectedSettings->showDeprecated, $settings->showDeprecated);
}

public function testColorsForced()
Expand Down
15 changes: 15 additions & 0 deletions tests/examples/example-05/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

class Foo
{

/**
* @var string
*/
private $bar;

public function Foo($bar)
{
$this->bar = $bar;
}
}