Skip to content

Commit

Permalink
add phpstan-strict-rules
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoll committed Apr 18, 2023
1 parent 27e064d commit 3824d02
Show file tree
Hide file tree
Showing 23 changed files with 186 additions and 151 deletions.
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
"phpmd/phpmd": "1.5.*||~2.6",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": "^9.5",
"sebastian/phpcpd": "*"
"sebastian/phpcpd": "*",
"phpstan/phpstan-strict-rules": "^1.5",
"phpstan/extension-installer": "^1.2",
"phpstan/phpstan-phpunit": "^1.3"
},
"autoload": {
"psr-0": {"PHPCodeBrowser\\": "src/"}
Expand All @@ -41,7 +44,7 @@
"demo": [
"@clean",
"php -c php.ini vendor/bin/phpunit -c phpunit.xml.dist",
"phpcpd -q --log-pmd=build/logs/pmd-cpd.xml src || true",
"phpcpd --log-pmd=build/logs/pmd-cpd.xml src || true",
"phpmd src xml cleancode,codesize,controversial,design,naming,unusedcode --reportfile build/logs/pmd.xml || true",
"phpcs -q --report-checkstyle=build/logs/checkstyle.xml || true",
"@browser"
Expand All @@ -66,7 +69,8 @@
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
"dealerdirect/phpcodesniffer-composer-installer": true,
"phpstan/extension-installer": true
}
}
}
57 changes: 51 additions & 6 deletions phpstan_baseline.neon
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#1 \\$element of method PHPCodeBrowser\\\\AbstractPlugin\\:\\:mapIssues\\(\\) expects DOMElement, DOMNode given\\.$#"
message: "#^Only booleans are allowed in a negated boolean, array\\<PHPCodeBrowser\\\\File\\> given\\.$#"
count: 1
path: src/PHPCodeBrowser/AbstractPlugin.php
path: src/PHPCodeBrowser/CLIController.php

-
message: "#^Call to an undefined method DOMNode\\:\\:getAttribute\\(\\)\\.$#"
message: "#^Only booleans are allowed in a negated boolean, mixed given\\.$#"
count: 4
path: src/PHPCodeBrowser/Command/RunCommand.php

-
message: "#^Only booleans are allowed in an if condition, mixed given\\.$#"
count: 1
path: src/PHPCodeBrowser/Command/RunCommand.php

-
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
count: 2
path: src/PHPCodeBrowser/Helper/IOHelper.php

-
message: "#^Only booleans are allowed in a ternary operator condition, DOMNode\\|null given\\.$#"
count: 1
path: src/PHPCodeBrowser/Plugins/ErrorCPD.php
path: src/PHPCodeBrowser/IssueXML.php

-
message: "#^Call to an undefined method DOMNode\\:\\:getAttribute\\(\\)\\.$#"
message: "#^Only booleans are allowed in an if condition, mixed given\\.$#"
count: 1
path: src/PHPCodeBrowser/Plugins/ErrorCoverage.php
path: src/PHPCodeBrowser/IssueXML.php

-
message: "#^Only booleans are allowed in a negated boolean, string given\\.$#"
count: 1
path: src/PHPCodeBrowser/Plugins/ErrorCRAP.php

-
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
count: 1
path: src/PHPCodeBrowser/View/ViewAbstract.php

-
message: "#^Only booleans are allowed in a negated boolean, string given\\.$#"
count: 2
path: src/PHPCodeBrowser/View/ViewAbstract.php

-
message: "#^Comparison operation \"\\<\" between 1 and int\\<2, max\\> is always true\\.$#"
Expand All @@ -35,7 +65,22 @@ parameters:
count: 1
path: src/PHPCodeBrowser/View/ViewReview.php

-
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
count: 1
path: src/PHPCodeBrowser/View/ViewReview.php

-
message: "#^Only booleans are allowed in a negated boolean, array given\\.$#"
count: 1
path: src/PHPCodeBrowser/View/ViewReview.php

-
message: "#^Result of && is always false\\.$#"
count: 2
path: src/PHPCodeBrowser/View/ViewReview.php

-
message: "#^Switch condition type \\(int\\<0, max\\>\\) does not match case condition 1 \\< \\$lineErrorCount \\(bool\\)\\.$#"
count: 1
path: src/PHPCodeBrowser/View/ViewReview.php
8 changes: 4 additions & 4 deletions src/PHPCodeBrowser/AbstractPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ public function getFilesWithIssues(): array
* This method provides a default behaviour an can be overloaded to
* implement special behavior for other plugins.
*
* @param DOMElement $element The XML plugin node with its errors
* @param string $filename Name of the file to return issues for.
* @param \DOMNode $element The XML plugin node with its errors
* @param string $filename Name of the file to return issues for.
*
* @return array array of issue objects.
* @return array<Issue>
*/
public function mapIssues(DOMElement $element, string $filename): array
public function mapIssues(\DOMNode $element, string $filename): array
{
$errorList = [];

Expand Down
3 changes: 2 additions & 1 deletion src/PHPCodeBrowser/CLIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function run(): void

// init needed classes
$viewReview = new ViewReview(
\getenv('PHPCB_TEMPLATE_DIR') ?: \dirname(__FILE__, 3).'/templates',
\getenv('PHPCB_TEMPLATE_DIR') ? \getenv('PHPCB_TEMPLATE_DIR') : \dirname(__FILE__, 3).'/templates',
$this->htmlOutputDir,
$this->ioHelper,
$this->phpSuffixes
Expand All @@ -255,6 +255,7 @@ public function run(): void

// conversion of XML file cc to cb format
foreach ($this->registeredPlugins as $className) {
/** @var AbstractPlugin $plugin */
$plugin = \array_key_exists($className, $this->pluginOptions) ? new $className(
$issueXml,
$this->pluginOptions[$className]
Expand Down
6 changes: 3 additions & 3 deletions src/PHPCodeBrowser/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ static function ($class) {
*
* @phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
*
* @return int|null null or 0 if everything went fine, or an error code
* @return int 0 if everything went fine, or an error code
*/
protected function execute(InputInterface $input, OutputInterface $output): ?int
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->checkErrors($input);

Expand Down Expand Up @@ -276,7 +276,7 @@ protected function disablePlugins(array $disabledPlugins, array $plugins): array
foreach ($plugins as $pluginKey => $plugin) {
$name = \substr($plugin, \strlen('Error'));

if (!\in_array(\strtolower($name), $disabledPlugins)) {
if (!\in_array(\strtolower($name), $disabledPlugins, true)) {
continue;
}

Expand Down
8 changes: 3 additions & 5 deletions src/PHPCodeBrowser/Helper/IOHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,12 @@ public function deleteDirectory(string $source): void
/**
* Copy a directory within all its items.
*
* @param string $source The source directory
* @param string $target The target to create
* @param array $exclude List of files / folders that should not be copyed
* @param string $source The source directory
* @param string $target The target to create
*
* @return void
*/
public function copyDirectory(string $source, string $target, array $exclude = []): void
public function copyDirectory(string $source, string $target): void
{
// first check for target itself
$this->createDirectory($target);
Expand All @@ -245,7 +244,6 @@ public function copyDirectory(string $source, string $target, array $exclude = [
// create folder recursive
if (!$iterator->isDot()
&& $iterator->isDir()
&& !\in_array($item, $exclude)
) {
$this->copyDirectory(
$source.'/'.$item,
Expand Down
9 changes: 5 additions & 4 deletions src/PHPCodeBrowser/Plugins/ErrorCPD.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ class ErrorCPD extends AbstractPlugin
/**
* Mapper method for this plugin.
*
* @param DOMElement $element The XML plugin node with its errors
* @param string $filename
* @param \DOMNode $element The XML plugin node with its errors
* @param string $filename
*
* @return array
* @return array<Issue>
*/
public function mapIssues(DOMElement $element, string $filename): array
public function mapIssues(\DOMNode $element, string $filename): array
{
/** @var DOMElement $parentNode */
$parentNode = $element->parentNode;
$files = $this->issueXml->query(
'file[@path="'.$filename.'"]',
Expand Down
8 changes: 4 additions & 4 deletions src/PHPCodeBrowser/Plugins/ErrorCRAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ class ErrorCRAP extends AbstractPlugin
* This method provides a default behaviour an can be overloaded to
* implement special behavior for other plugins.
*
* @param DOMElement $element The XML plugin node with its errors
* @param string $filename Name of the file to return issues for.
* @param \DOMNode $element The XML plugin node with its errors
* @param string $filename Name of the file to return issues for.
*
* @return array array of issue objects.
* @return array<Issue>
*/
public function mapIssues(DOMElement $element, string $filename): array
public function mapIssues(\DOMNode $element, string $filename): array
{
$errorList = [];

Expand Down
9 changes: 5 additions & 4 deletions src/PHPCodeBrowser/Plugins/ErrorCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,20 @@ class ErrorCoverage extends AbstractPlugin
* This method provides a default behaviour an can be overloaded to
* implement special behavior for other plugins.
*
* @param DOMElement $element The XML plugin node with its errors
* @param string $filename Name of the file to return issues for.
* @param DOMNode $element The XML plugin node with its errors
* @param string $filename Name of the file to return issues for.
*
* @return array array of issue objects.
* @return array<Issue>
*/
public function mapIssues(DOMElement $element, string $filename): array
public function mapIssues(DOMNode $element, string $filename): array
{
$errorList = [];

$children = $element->childNodes;
$childCount = $children->length;

for ($next = 0; $next < $childCount; ++$next) {
/** @var DOMElement $child */
$child = $children->item($next);

if (!$this->representsUncoveredLOC($child)) {
Expand Down
2 changes: 1 addition & 1 deletion src/PHPCodeBrowser/SourceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function addSourceFile($file): void
$filename = $file;
$file = \realpath($file);
} else {
$filename = $file->getPathName();
$filename = $file->getPathname();
$file = $file->getRealPath();
}

Expand Down
4 changes: 2 additions & 2 deletions src/PHPCodeBrowser/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function setUp(): void
*/
public function testCommand(): void
{
$this->assertInstanceOf(RunCommand::class, $this->application->get('phpcb'));
static::assertInstanceOf(RunCommand::class, $this->application->get('phpcb'));
}

/**
Expand All @@ -40,6 +40,6 @@ public function testGetDefinitionClearsArguments(): void
{
$this->application->getDefinition()->setArguments([new InputArgument('foo')]);

$this->assertEquals(0, $this->application->getDefinition()->getArgumentCount());
static::assertEquals(0, $this->application->getDefinition()->getArgumentCount());
}
}
16 changes: 8 additions & 8 deletions src/PHPCodeBrowser/Tests/CLIControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public function testRunCleansExistingOutputDir(): void
$this->controller->run();

$this->assertOutputIsPresent();
$this->assertDirectoryDoesNotExist(self::$testOutputDir.'/clear-directory');
$this->assertFileDoesNotExist(self::$testOutputDir.'/clear-file');
static::assertDirectoryDoesNotExist(self::$testOutputDir.'/clear-directory');
static::assertFileDoesNotExist(self::$testOutputDir.'/clear-file');
}

/**
Expand Down Expand Up @@ -183,11 +183,11 @@ public function testRunExcludingAllSources(): void

$this->controller->run();

$this->assertFileExists(self::$testOutputDir.'/index.html');
$this->assertFileDoesNotExist(self::$testOutputDir.'/Bad.php.html');
$this->assertFileDoesNotExist(self::$testOutputDir.'/Good.php.html');
$this->assertDirectoryDoesNotExist(self::$testOutputDir.'/css');
$this->assertDirectoryDoesNotExist(self::$testOutputDir.'/img');
$this->assertDirectoryDoesNotExist(self::$testOutputDir.'/js');
static::assertFileExists(self::$testOutputDir.'/index.html');
static::assertFileDoesNotExist(self::$testOutputDir.'/Bad.php.html');
static::assertFileDoesNotExist(self::$testOutputDir.'/Good.php.html');
static::assertDirectoryDoesNotExist(self::$testOutputDir.'/css');
static::assertDirectoryDoesNotExist(self::$testOutputDir.'/img');
static::assertDirectoryDoesNotExist(self::$testOutputDir.'/js');
}
}
Loading

0 comments on commit 3824d02

Please sign in to comment.