Skip to content

Commit 3824d02

Browse files
committed
add phpstan-strict-rules
1 parent 27e064d commit 3824d02

23 files changed

+186
-151
lines changed

Diff for: composer.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
"phpmd/phpmd": "1.5.*||~2.6",
3232
"phpstan/phpstan": "^1.0",
3333
"phpunit/phpunit": "^9.5",
34-
"sebastian/phpcpd": "*"
34+
"sebastian/phpcpd": "*",
35+
"phpstan/phpstan-strict-rules": "^1.5",
36+
"phpstan/extension-installer": "^1.2",
37+
"phpstan/phpstan-phpunit": "^1.3"
3538
},
3639
"autoload": {
3740
"psr-0": {"PHPCodeBrowser\\": "src/"}
@@ -41,7 +44,7 @@
4144
"demo": [
4245
"@clean",
4346
"php -c php.ini vendor/bin/phpunit -c phpunit.xml.dist",
44-
"phpcpd -q --log-pmd=build/logs/pmd-cpd.xml src || true",
47+
"phpcpd --log-pmd=build/logs/pmd-cpd.xml src || true",
4548
"phpmd src xml cleancode,codesize,controversial,design,naming,unusedcode --reportfile build/logs/pmd.xml || true",
4649
"phpcs -q --report-checkstyle=build/logs/checkstyle.xml || true",
4750
"@browser"
@@ -66,7 +69,8 @@
6669
},
6770
"config": {
6871
"allow-plugins": {
69-
"dealerdirect/phpcodesniffer-composer-installer": true
72+
"dealerdirect/phpcodesniffer-composer-installer": true,
73+
"phpstan/extension-installer": true
7074
}
7175
}
7276
}

Diff for: phpstan_baseline.neon

+51-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,49 @@
11
parameters:
22
ignoreErrors:
33
-
4-
message: "#^Parameter \\#1 \\$element of method PHPCodeBrowser\\\\AbstractPlugin\\:\\:mapIssues\\(\\) expects DOMElement, DOMNode given\\.$#"
4+
message: "#^Only booleans are allowed in a negated boolean, array\\<PHPCodeBrowser\\\\File\\> given\\.$#"
55
count: 1
6-
path: src/PHPCodeBrowser/AbstractPlugin.php
6+
path: src/PHPCodeBrowser/CLIController.php
77

88
-
9-
message: "#^Call to an undefined method DOMNode\\:\\:getAttribute\\(\\)\\.$#"
9+
message: "#^Only booleans are allowed in a negated boolean, mixed given\\.$#"
10+
count: 4
11+
path: src/PHPCodeBrowser/Command/RunCommand.php
12+
13+
-
14+
message: "#^Only booleans are allowed in an if condition, mixed given\\.$#"
15+
count: 1
16+
path: src/PHPCodeBrowser/Command/RunCommand.php
17+
18+
-
19+
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
20+
count: 2
21+
path: src/PHPCodeBrowser/Helper/IOHelper.php
22+
23+
-
24+
message: "#^Only booleans are allowed in a ternary operator condition, DOMNode\\|null given\\.$#"
1025
count: 1
11-
path: src/PHPCodeBrowser/Plugins/ErrorCPD.php
26+
path: src/PHPCodeBrowser/IssueXML.php
1227

1328
-
14-
message: "#^Call to an undefined method DOMNode\\:\\:getAttribute\\(\\)\\.$#"
29+
message: "#^Only booleans are allowed in an if condition, mixed given\\.$#"
1530
count: 1
16-
path: src/PHPCodeBrowser/Plugins/ErrorCoverage.php
31+
path: src/PHPCodeBrowser/IssueXML.php
32+
33+
-
34+
message: "#^Only booleans are allowed in a negated boolean, string given\\.$#"
35+
count: 1
36+
path: src/PHPCodeBrowser/Plugins/ErrorCRAP.php
37+
38+
-
39+
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
40+
count: 1
41+
path: src/PHPCodeBrowser/View/ViewAbstract.php
42+
43+
-
44+
message: "#^Only booleans are allowed in a negated boolean, string given\\.$#"
45+
count: 2
46+
path: src/PHPCodeBrowser/View/ViewAbstract.php
1747

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

68+
-
69+
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
70+
count: 1
71+
path: src/PHPCodeBrowser/View/ViewReview.php
72+
73+
-
74+
message: "#^Only booleans are allowed in a negated boolean, array given\\.$#"
75+
count: 1
76+
path: src/PHPCodeBrowser/View/ViewReview.php
77+
3878
-
3979
message: "#^Result of && is always false\\.$#"
4080
count: 2
4181
path: src/PHPCodeBrowser/View/ViewReview.php
82+
83+
-
84+
message: "#^Switch condition type \\(int\\<0, max\\>\\) does not match case condition 1 \\< \\$lineErrorCount \\(bool\\)\\.$#"
85+
count: 1
86+
path: src/PHPCodeBrowser/View/ViewReview.php

Diff for: src/PHPCodeBrowser/AbstractPlugin.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,12 @@ public function getFilesWithIssues(): array
217217
* This method provides a default behaviour an can be overloaded to
218218
* implement special behavior for other plugins.
219219
*
220-
* @param DOMElement $element The XML plugin node with its errors
221-
* @param string $filename Name of the file to return issues for.
220+
* @param \DOMNode $element The XML plugin node with its errors
221+
* @param string $filename Name of the file to return issues for.
222222
*
223-
* @return array array of issue objects.
223+
* @return array<Issue>
224224
*/
225-
public function mapIssues(DOMElement $element, string $filename): array
225+
public function mapIssues(\DOMNode $element, string $filename): array
226226
{
227227
$errorList = [];
228228

Diff for: src/PHPCodeBrowser/CLIController.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public function run(): void
239239

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

256256
// conversion of XML file cc to cb format
257257
foreach ($this->registeredPlugins as $className) {
258+
/** @var AbstractPlugin $plugin */
258259
$plugin = \array_key_exists($className, $this->pluginOptions) ? new $className(
259260
$issueXml,
260261
$this->pluginOptions[$className]

Diff for: src/PHPCodeBrowser/Command/RunCommand.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ static function ($class) {
156156
*
157157
* @phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
158158
*
159-
* @return int|null null or 0 if everything went fine, or an error code
159+
* @return int 0 if everything went fine, or an error code
160160
*/
161-
protected function execute(InputInterface $input, OutputInterface $output): ?int
161+
protected function execute(InputInterface $input, OutputInterface $output): int
162162
{
163163
$this->checkErrors($input);
164164

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

279-
if (!\in_array(\strtolower($name), $disabledPlugins)) {
279+
if (!\in_array(\strtolower($name), $disabledPlugins, true)) {
280280
continue;
281281
}
282282

Diff for: src/PHPCodeBrowser/Helper/IOHelper.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,12 @@ public function deleteDirectory(string $source): void
222222
/**
223223
* Copy a directory within all its items.
224224
*
225-
* @param string $source The source directory
226-
* @param string $target The target to create
227-
* @param array $exclude List of files / folders that should not be copyed
225+
* @param string $source The source directory
226+
* @param string $target The target to create
228227
*
229228
* @return void
230229
*/
231-
public function copyDirectory(string $source, string $target, array $exclude = []): void
230+
public function copyDirectory(string $source, string $target): void
232231
{
233232
// first check for target itself
234233
$this->createDirectory($target);
@@ -245,7 +244,6 @@ public function copyDirectory(string $source, string $target, array $exclude = [
245244
// create folder recursive
246245
if (!$iterator->isDot()
247246
&& $iterator->isDir()
248-
&& !\in_array($item, $exclude)
249247
) {
250248
$this->copyDirectory(
251249
$source.'/'.$item,

Diff for: src/PHPCodeBrowser/Plugins/ErrorCPD.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,14 @@ class ErrorCPD extends AbstractPlugin
8888
/**
8989
* Mapper method for this plugin.
9090
*
91-
* @param DOMElement $element The XML plugin node with its errors
92-
* @param string $filename
91+
* @param \DOMNode $element The XML plugin node with its errors
92+
* @param string $filename
9393
*
94-
* @return array
94+
* @return array<Issue>
9595
*/
96-
public function mapIssues(DOMElement $element, string $filename): array
96+
public function mapIssues(\DOMNode $element, string $filename): array
9797
{
98+
/** @var DOMElement $parentNode */
9899
$parentNode = $element->parentNode;
99100
$files = $this->issueXml->query(
100101
'file[@path="'.$filename.'"]',

Diff for: src/PHPCodeBrowser/Plugins/ErrorCRAP.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ class ErrorCRAP extends AbstractPlugin
115115
* This method provides a default behaviour an can be overloaded to
116116
* implement special behavior for other plugins.
117117
*
118-
* @param DOMElement $element The XML plugin node with its errors
119-
* @param string $filename Name of the file to return issues for.
118+
* @param \DOMNode $element The XML plugin node with its errors
119+
* @param string $filename Name of the file to return issues for.
120120
*
121-
* @return array array of issue objects.
121+
* @return array<Issue>
122122
*/
123-
public function mapIssues(DOMElement $element, string $filename): array
123+
public function mapIssues(\DOMNode $element, string $filename): array
124124
{
125125
$errorList = [];
126126

Diff for: src/PHPCodeBrowser/Plugins/ErrorCoverage.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,20 @@ class ErrorCoverage extends AbstractPlugin
118118
* This method provides a default behaviour an can be overloaded to
119119
* implement special behavior for other plugins.
120120
*
121-
* @param DOMElement $element The XML plugin node with its errors
122-
* @param string $filename Name of the file to return issues for.
121+
* @param DOMNode $element The XML plugin node with its errors
122+
* @param string $filename Name of the file to return issues for.
123123
*
124-
* @return array array of issue objects.
124+
* @return array<Issue>
125125
*/
126-
public function mapIssues(DOMElement $element, string $filename): array
126+
public function mapIssues(DOMNode $element, string $filename): array
127127
{
128128
$errorList = [];
129129

130130
$children = $element->childNodes;
131131
$childCount = $children->length;
132132

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

136137
if (!$this->representsUncoveredLOC($child)) {

Diff for: src/PHPCodeBrowser/SourceHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function addSourceFile($file): void
153153
$filename = $file;
154154
$file = \realpath($file);
155155
} else {
156-
$filename = $file->getPathName();
156+
$filename = $file->getPathname();
157157
$file = $file->getRealPath();
158158
}
159159

Diff for: src/PHPCodeBrowser/Tests/ApplicationTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected function setUp(): void
3030
*/
3131
public function testCommand(): void
3232
{
33-
$this->assertInstanceOf(RunCommand::class, $this->application->get('phpcb'));
33+
static::assertInstanceOf(RunCommand::class, $this->application->get('phpcb'));
3434
}
3535

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

43-
$this->assertEquals(0, $this->application->getDefinition()->getArgumentCount());
43+
static::assertEquals(0, $this->application->getDefinition()->getArgumentCount());
4444
}
4545
}

Diff for: src/PHPCodeBrowser/Tests/CLIControllerTest.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ public function testRunCleansExistingOutputDir(): void
147147
$this->controller->run();
148148

149149
$this->assertOutputIsPresent();
150-
$this->assertDirectoryDoesNotExist(self::$testOutputDir.'/clear-directory');
151-
$this->assertFileDoesNotExist(self::$testOutputDir.'/clear-file');
150+
static::assertDirectoryDoesNotExist(self::$testOutputDir.'/clear-directory');
151+
static::assertFileDoesNotExist(self::$testOutputDir.'/clear-file');
152152
}
153153

154154
/**
@@ -183,11 +183,11 @@ public function testRunExcludingAllSources(): void
183183

184184
$this->controller->run();
185185

186-
$this->assertFileExists(self::$testOutputDir.'/index.html');
187-
$this->assertFileDoesNotExist(self::$testOutputDir.'/Bad.php.html');
188-
$this->assertFileDoesNotExist(self::$testOutputDir.'/Good.php.html');
189-
$this->assertDirectoryDoesNotExist(self::$testOutputDir.'/css');
190-
$this->assertDirectoryDoesNotExist(self::$testOutputDir.'/img');
191-
$this->assertDirectoryDoesNotExist(self::$testOutputDir.'/js');
186+
static::assertFileExists(self::$testOutputDir.'/index.html');
187+
static::assertFileDoesNotExist(self::$testOutputDir.'/Bad.php.html');
188+
static::assertFileDoesNotExist(self::$testOutputDir.'/Good.php.html');
189+
static::assertDirectoryDoesNotExist(self::$testOutputDir.'/css');
190+
static::assertDirectoryDoesNotExist(self::$testOutputDir.'/img');
191+
static::assertDirectoryDoesNotExist(self::$testOutputDir.'/js');
192192
}
193193
}

0 commit comments

Comments
 (0)