Skip to content

Commit

Permalink
Merge pull request #113 from DaveLiddament/feature/add-php-magic-number
Browse files Browse the repository at this point in the history
ADD support for PHP Magic Number Detector
  • Loading branch information
DaveLiddament authored Dec 23, 2021
2 parents 8eec5ee + 5403014 commit 3d84218
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ vendor/bin/phpstan analyse --error-format=json | vendor/bin/sarb remove phpstan.
```
NOTE: Checkout PHPStan's built in [baseline feature](https://phpstan.org/user-guide/baseline). Learn how [it differs from SARB](docs/SarbVsOtherBaseliningTechniques.md).

#### [PHP Magic Number Detector](https://github.com/povils/phpmnd)
```shell
vendor/bin/phpmnd . | vendor/bin/sarb create --input-format="phpmnd" phpmnd.baseline
vendor/bin/phpmnd . | vendor/bin/sarb remove phpmnd.baseline
```

See notes on [relative paths](docs/ResultsWithRelativePaths.md).

## My tool isn't supported...

That's no problem there are 3 methods to [integrate a static analysis tool](docs/CustomInputFormats.md) with SARB.
Expand Down
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>';
}
}
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;
}
}
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()
);
}
}
19 changes: 19 additions & 0 deletions tests/resources/phpmnd/phpmnd.txt
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

0 comments on commit 3d84218

Please sign in to comment.