Skip to content

Commit

Permalink
add phpcs strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
exussum12 committed Feb 26, 2017
1 parent b697608 commit 33f5153
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ phpcs can be run with any options you normally have for example `--standard=PSR2

This will exit with code 2 if any of the new/edited code fails the code standards check. The output is kept so you can see what the offending lines are and what the error is.

Strict mode turns warning to errors


##phpmd

Expand Down
22 changes: 17 additions & 5 deletions src/PhpCsLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class PhpCsLoader implements FileChecker
*/
protected $invalidLines;

/**
* @var array
*/

protected $failOnTypes = [
'ERROR',
];

/**
* PhpCsLoader constructor.
* @param $filePath the file path to the json output from phpcs
Expand All @@ -41,7 +49,7 @@ public function getLines()
$this->invalidLines = [];
foreach ($this->json->files as $fileName => $file) {
foreach ($file->messages as $message) {
$this->addInvalidLine($fileName, $message->line, $message->message);
$this->addInvalidLine($fileName, $message);
}
}

Expand All @@ -58,15 +66,19 @@ public function isValidLine($file, $lineNumber)

/**
* @param string $file
* @param int $line
* @param string $error
* @param stdClass $message
*/
protected function addInvalidLine($file, $line, $error)
protected function addInvalidLine($file, $message)
{
if (!in_array($message->type, $this->failOnTypes)) {
return;
}
$line = $message->line;

if (!isset($this->invalidLines[$file][$line])) {
$this->invalidLines[$file][$line] = [];
}
$this->invalidLines[$file][$line][] = $error;
$this->invalidLines[$file][$line][] = $message->message;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/PhpCsLoaderStrict.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace exussum12\CoverageChecker;

use InvalidArgumentException;

/**
* Class PhpCsLoaderStrict
* Used to fail warnings too
* @package exussum12\CoverageChecker
*/
class PhpCsLoaderStrict extends PhpCsLoader
{
protected $failOnTypes = [
'ERROR',
'WARNING',
];
}
17 changes: 17 additions & 0 deletions tests/LoadPhpcsReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use PHPUnit\Framework\TestCase;
use exussum12\CoverageChecker\PhpCsLoader;
use exussum12\CoverageChecker\PhpCsLoaderStrict;

class LoadPhpcsReportTest extends TestCase
{
Expand Down Expand Up @@ -36,4 +37,20 @@ public function testCorrectMissingFile()

$this->assertTrue($phpcs->handleNotFoundFile());
}

public function testStrictMode()
{
$phpcs = new PhpCsLoaderStrict(__DIR__ . '/fixtures/phpcsstrict.json');
$invalidLines = $phpcs->getLines();
$expected = [
'/full/path/to/file/src/XMLReport.php' => [
11 => ['Opening brace should be on the line after the declaration; found 1 blank line(s)'],
12 => ['Line indented incorrectly; expected at least 8 spaces, found 4'],
],
];

$this->assertEquals($expected, $invalidLines);
$this->assertFalse($phpcs->isValidLine('/full/path/to/file/src/XMLReport.php', 11));
$this->assertTrue($phpcs->isValidLine('/full/path/to/file/src/XMLReport.php', 10));
}
}
13 changes: 13 additions & 0 deletions tests/PhpcsDiffFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,17 @@ public function testValid()
$output = ob_get_clean();
$this->assertContains('100.00%', $output);
}

public function testStrictMode()
{
$GLOBALS['argv'] = [
'phpunitDiffFilter',
__DIR__ . '/fixtures/change.txt',
__DIR__ . '/fixtures/phpcsstrict.json'
];
ob_start();
require(__DIR__ . "/../src/runners/phpcsDiffFilter.php");
$output = ob_get_clean();
$this->assertContains('100.00%', $output);
}
}
1 change: 1 addition & 0 deletions tests/fixtures/phpcsstrict.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"totals":{"errors":1,"warnings":1,"fixable":2},"files":{"\/full\/path\/to\/file\/src\/XMLReport.php":{"errors":2,"warnings":0,"messages":[{"message":"Opening brace should be on the line after the declaration; found 1 blank line(s)","source":"Squiz.Functions.MultiLineFunctionDeclaration.BraceSpacing","severity":5,"type":"ERROR","line":11,"column":5,"fixable":true},{"message":"Line indented incorrectly; expected at least 8 spaces, found 4","source":"Generic.WhiteSpace.ScopeIndent.Incorrect","severity":5,"type":"WARNING","line":12,"column":5,"fixable":true}]},"\/full\/path\/to\/file\/src\/DiffFileState.php":{"errors":0,"warnings":0,"messages":[]},"\/full\/path\/to\/file\/src\/DiffLineHandle\/AddedLine.php":{"errors":0,"warnings":0,"messages":[]},"\/full\/path\/to\/file\/src\/DiffLineHandle\/NewFile.php":{"errors":0,"warnings":0,"messages":[]},"\/full\/path\/to\/file\/src\/DiffLineHandle\/RemovedLine.php":{"errors":0,"warnings":0,"messages":[]},"\/full\/path\/to\/file\/src\/DiffLineHandle\/DiffStart.php":{"errors":0,"warnings":0,"messages":[]},"\/full\/path\/to\/file\/src\/DiffLineHandle\/ContextLine.php":{"errors":0,"warnings":0,"messages":[]},"\/full\/path\/to\/file\/src\/CoverageCheck.php":{"errors":0,"warnings":0,"messages":[]},"\/full\/path\/to\/file\/src\/PhpCsloader.php":{"errors":0,"warnings":0,"messages":[]},"\/full\/path\/to\/file\/src\/FileMatchers\/EndsWith.php":{"errors":0,"warnings":0,"messages":[]},"\/full\/path\/to\/file\/src\/DiffFileLoader.php":{"errors":0,"warnings":0,"messages":[]},"\/full\/path\/to\/file\/src\/DiffLineHandle.php":{"errors":0,"warnings":0,"messages":[]},"\/full\/path\/to\/file\/src\/Exceptions\/FileNotFound.php":{"errors":0,"warnings":0,"messages":[]},"\/full\/path\/to\/file\/src\/FileMatcher.php":{"errors":0,"warnings":0,"messages":[]}}}

0 comments on commit 33f5153

Please sign in to comment.