-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from DaveLiddament/feature/add-php-magic-number
ADD support for PHP Magic Number Detector
- Loading branch information
Showing
5 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...s/ResultsParsers/PhpMagicNumberDetectorResultsParser/PhpMagicNumberDetectorIdentifier.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/** | ||
* Static Analysis Results Baseliner (sarb). | ||
* | ||
* (c) Dave Liddament | ||
* | ||
* For the full copyright and licence information please view the LICENSE file distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\PhpMagicNumberDetectorResultsParser; | ||
|
||
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\Identifier; | ||
|
||
class PhpMagicNumberDetectorIdentifier implements Identifier | ||
{ | ||
public function getCode(): string | ||
{ | ||
return 'phpmnd'; | ||
} | ||
|
||
public function getDescription(): string | ||
{ | ||
return 'PHP Magic Number Detector'; | ||
} | ||
|
||
public function getToolCommand(): string | ||
{ | ||
return 'phpmnd <files|directories to scan>'; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...esultsParsers/PhpMagicNumberDetectorResultsParser/PhpMagicNumberDetectorResultsParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
/** | ||
* Static Analysis Results Baseliner (sarb). | ||
* | ||
* (c) Dave Liddament | ||
* | ||
* For the full copyright and licence information please view the LICENSE file distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\PhpMagicNumberDetectorResultsParser; | ||
|
||
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\LineNumber; | ||
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\Location; | ||
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\ProjectRoot; | ||
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\RelativeFileName; | ||
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\Type; | ||
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResult; | ||
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResults; | ||
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResultsBuilder; | ||
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\Identifier; | ||
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\ResultsParser; | ||
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\ArrayUtils; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* Handles PHPMND CLI output. | ||
*/ | ||
class PhpMagicNumberDetectorResultsParser implements ResultsParser | ||
{ | ||
private const MAGIC_NUMBER_REGEX = "/^(.*):(\d+)\. Magic number: (.*)$/"; | ||
|
||
public function convertFromString(string $resultsAsString, ProjectRoot $projectRoot): AnalysisResults | ||
{ | ||
$lines = explode(\PHP_EOL, $resultsAsString); | ||
$analysisResultsBuilder = new AnalysisResultsBuilder(); | ||
|
||
foreach ($lines as $line) { | ||
$matches = null; | ||
$match = preg_match(self::MAGIC_NUMBER_REGEX, $line, $matches); | ||
if (1 === $match) { | ||
Assert::count($matches, 4); | ||
$relativeFileName = ArrayUtils::getStringValue($matches, '1'); | ||
$lineNumber = ArrayUtils::getIntAsStringValue($matches, '2'); | ||
$magicNumber = ArrayUtils::getStringValue($matches, '3'); | ||
|
||
$location = Location::fromRelativeFileName( | ||
new RelativeFileName($relativeFileName), | ||
$projectRoot, | ||
new LineNumber($lineNumber) | ||
); | ||
|
||
$analysisResult = new AnalysisResult( | ||
$location, | ||
new Type($magicNumber), | ||
"Magic number {$magicNumber}", | ||
[] | ||
); | ||
|
||
$analysisResultsBuilder->addAnalysisResult($analysisResult); | ||
} | ||
} | ||
|
||
return $analysisResultsBuilder->build(); | ||
} | ||
|
||
public function getIdentifier(): Identifier | ||
{ | ||
return new PhpMagicNumberDetectorIdentifier(); | ||
} | ||
|
||
public function showTypeGuessingWarning(): bool | ||
{ | ||
return false; | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...ns/ResultsParsers/PhpMagicNumberDetectorResultsParser/PhpMagicNumberResultsParserTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Unit\Plugins\ResultsParsers\PhpMagicNumberDetectorResultsParser; | ||
|
||
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\ProjectRoot; | ||
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResults; | ||
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\PhpMagicNumberDetectorResultsParser\PhpMagicNumberDetectorIdentifier; | ||
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\PhpMagicNumberDetectorResultsParser\PhpMagicNumberDetectorResultsParser; | ||
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\AssertFileContentsSameTrait; | ||
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\AssertResultMatch; | ||
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\ResourceLoaderTrait; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PhpMagicNumberResultsParserTest extends TestCase | ||
{ | ||
use AssertFileContentsSameTrait; | ||
use AssertResultMatch; | ||
use ResourceLoaderTrait; | ||
|
||
/** | ||
* @var AnalysisResults | ||
*/ | ||
private $analysisResults; | ||
|
||
/** | ||
* @var PhpMagicNumberDetectorResultsParser | ||
*/ | ||
private $phpMagicNumberDetectorResultsParser; | ||
|
||
protected function setUp(): void | ||
{ | ||
$projectRoot = ProjectRoot::fromProjectRoot('/vagrant/static-analysis-baseliner', '/home'); | ||
|
||
$this->phpMagicNumberDetectorResultsParser = new PhpMagicNumberDetectorResultsParser(); | ||
$original = $this->getResource('phpmnd/phpmnd.txt'); | ||
|
||
// Convert both ways | ||
$this->analysisResults = $this->phpMagicNumberDetectorResultsParser->convertFromString($original, $projectRoot); | ||
} | ||
|
||
public function testConversion(): void | ||
{ | ||
$this->assertCount(3, $this->analysisResults->getAnalysisResults()); | ||
|
||
$result1 = $this->analysisResults->getAnalysisResults()[0]; | ||
$result2 = $this->analysisResults->getAnalysisResults()[1]; | ||
$result3 = $this->analysisResults->getAnalysisResults()[2]; | ||
|
||
$this->assertMatch($result1, | ||
'src/File1.php', | ||
6, | ||
'123' | ||
); | ||
$this->assertSame( | ||
'Magic number 123', | ||
$result1->getMessage() | ||
); | ||
|
||
$this->assertMatch($result2, | ||
'src/foo/bar/File 2.php', | ||
11, | ||
'1.7' | ||
); | ||
|
||
$this->assertMatch($result3, | ||
'tests/MyTest1.php', | ||
16, | ||
'10' | ||
); | ||
} | ||
|
||
public function testTypeGuesser(): void | ||
{ | ||
$this->assertFalse($this->phpMagicNumberDetectorResultsParser->showTypeGuessingWarning()); | ||
} | ||
|
||
public function testGetIdentifier(): void | ||
{ | ||
$this->assertEquals( | ||
new PhpMagicNumberDetectorIdentifier(), | ||
$this->phpMagicNumberDetectorResultsParser->getIdentifier() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
phpmnd version 2.5.0 by Povilas Susinskas | ||
|
||
|
||
|
||
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
src/File1.php:6. Magic number: 123 | ||
> 6| return "123"; | ||
|
||
src/foo/bar/File 2.php:11. Magic number: 1.7 | ||
> 11| return 1.7; | ||
|
||
tests/MyTest1.php:16. Magic number: 10 | ||
> 16| return 10; | ||
|
||
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
Total of Magic Numbers: 3 | ||
Time: 00:00.058, Memory: 8.00 MB |