Skip to content

Commit e208e2c

Browse files
authored
Updated coding standard (#27)
1 parent d4c1376 commit e208e2c

10 files changed

+41
-25
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"phpstan/phpstan-phpunit": "^0.10",
2424
"phpstan/phpstan-nette": "^0.10",
2525
"phpstan/phpstan-strict-rules": "^0.10",
26-
"slevomat/coding-standard": "^4.5.2",
26+
"slevomat/coding-standard": "^4.8.3",
2727
"satooshi/php-coveralls": "^2.0",
28-
"squizlabs/php_codesniffer": "~3.2.3"
28+
"squizlabs/php_codesniffer": "~3.3.2"
2929
},
3030
"autoload": {
3131
"psr-4": {

phpcs.xml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,17 @@
179179
<severity>5</severity><!-- turned off by PSR2 -> turning on with default severity -->
180180
</rule>
181181

182+
<rule ref="SlevomatCodingStandard.Arrays.DisallowImplicitArrayCreation"/>
182183
<rule ref="SlevomatCodingStandard.Arrays.TrailingArrayComma"/>
183184
<rule ref="SlevomatCodingStandard.Classes.ClassConstantVisibility" />
185+
<rule ref="SlevomatCodingStandard.Classes.DisallowLateStaticBindingForConstants"/>
184186
<rule ref="SlevomatCodingStandard.Classes.UnusedPrivateElements"/>
187+
<rule ref="SlevomatCodingStandard.Classes.UselessLateStaticBinding"/>
188+
<rule ref="SlevomatCodingStandard.Classes.ModernClassNameReference"/>
189+
<rule ref="SlevomatCodingStandard.Classes.TraitUseDeclaration"/>
190+
<rule ref="SlevomatCodingStandard.Classes.TraitUseSpacing"/>
185191
<rule ref="SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration"/>
192+
<rule ref="SlevomatCodingStandard.Commenting.UselessInheritDocComment"/>
186193
<rule ref="SlevomatCodingStandard.Commenting.ForbiddenAnnotations">
187194
<properties>
188195
<property name="forbiddenAnnotations" type="array"
@@ -193,10 +200,17 @@
193200
<rule ref="SlevomatCodingStandard.ControlStructures.LanguageConstructWithParentheses"/>
194201
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowYodaComparison"/>
195202
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowEqualOperators" />
203+
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowContinueWithoutIntegerOperandInSwitch" />
196204
<rule ref="SlevomatCodingStandard.ControlStructures.NewWithParentheses"/>
197205
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator"/>
206+
<rule ref="SlevomatCodingStandard.ControlStructures.RequireTernaryOperator"/>
207+
<rule ref="SlevomatCodingStandard.ControlStructures.EarlyExit"/>
208+
<rule ref="SlevomatCodingStandard.ControlStructures.UselessConditionWithReturn"/>
198209
<rule ref="SlevomatCodingStandard.Exceptions.DeadCatch"/>
199210
<rule ref="SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly" />
211+
<rule ref="SlevomatCodingStandard.Functions.UnusedInheritedVariablePassedToClosure"/>
212+
<rule ref="SlevomatCodingStandard.Functions.UselessParameterDefaultValue"/>
213+
<rule ref="SlevomatCodingStandard.Functions.StaticClosure"/>
200214
<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
201215
<properties>
202216
<property name="rootNamespaces" type="array" value="src=>Pepakriz\PHPStanExceptionRules,tests/src=>Pepakriz\PHPStanExceptionRules"/>
@@ -212,6 +226,7 @@
212226
<rule ref="SlevomatCodingStandard.Namespaces.DisallowGroupUse"/>
213227
<rule ref="SlevomatCodingStandard.Namespaces.MultipleUsesPerLine"/>
214228
<rule ref="SlevomatCodingStandard.Namespaces.UseDoesNotStartWithBackslash"/>
229+
<rule ref="SlevomatCodingStandard.Namespaces.UselessAlias"/>
215230
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
216231
<properties>
217232
<property name="searchAnnotations" type="bool" value="true"/>
@@ -228,6 +243,11 @@
228243
<property name="allowFallbackGlobalConstants" type="bool" value="false"/>
229244
</properties>
230245
</rule>
246+
<rule ref="SlevomatCodingStandard.Operators.RequireOnlyStandaloneIncrementAndDecrementOperators"/>
247+
<rule ref="SlevomatCodingStandard.Operators.RequireCombinedAssignmentOperator"/>
248+
<rule ref="SlevomatCodingStandard.PHP.UselessParentheses"/>
249+
<rule ref="SlevomatCodingStandard.PHP.OptimizedFunctionsWithoutUnpacking"/>
250+
<rule ref="SlevomatCodingStandard.PHP.UselessSemicolon"/>
231251
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
232252
<properties>
233253
<property name="newlinesCountBetweenOpenTagAndDeclare" value="0"/>
@@ -251,11 +271,11 @@
251271
<rule ref="SlevomatCodingStandard.TypeHints.LongTypeHints"/>
252272
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing"/>
253273
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHintSpacing"/>
274+
<rule ref="SlevomatCodingStandard.TypeHints.UselessConstantTypeHint"/>
254275
<rule ref="SlevomatCodingStandard.Types.EmptyLinesAroundTypeBraces"/>
255-
<rule ref="SlevomatCodingStandard.ControlStructures.AssignmentInCondition"/>
256-
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowEqualOperators"/>
257-
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator"/>
258-
<rule ref="SlevomatCodingStandard.ControlStructures.EarlyExit"/>
276+
<rule ref="SlevomatCodingStandard.Variables.DuplicateAssignmentToVariable"/>
277+
<rule ref="SlevomatCodingStandard.Variables.UnusedVariable"/>
278+
<rule ref="SlevomatCodingStandard.Variables.UselessVariable"/>
259279

260280
<rule ref="Cdn77CodingStandard.Classes.ClassStructure"/>
261281
<rule ref="Cdn77CodingStandard.Classes.ClassUsesSeparately"/>

src/DI/ExceptionRulesExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private function getServicesByNames(string $tag): array
3737
$containerBuilder = $this->getContainerBuilder();
3838
$names = array_keys($containerBuilder->findByTag($tag));
3939

40-
return array_map(function (string $name) use ($containerBuilder): ServiceDefinition {
40+
return array_map(static function (string $name) use ($containerBuilder): ServiceDefinition {
4141
return $containerBuilder->getDefinition($name);
4242
}, $names);
4343
}

src/DefaultThrowTypeExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct(
4646
}
4747

4848
$this->methodThrowTypes[$className][$methodName] = TypeCombinator::union(
49-
...array_map(function (string $throwType): ObjectType {
49+
...array_map(static function (string $throwType): ObjectType {
5050
return new ObjectType($throwType);
5151
}, $throwTypes)
5252
);
@@ -60,7 +60,7 @@ public function __construct(
6060
}
6161

6262
$this->functionThrowTypes[$functionName] = TypeCombinator::union(
63-
...array_map(function (string $throwType): ObjectType {
63+
...array_map(static function (string $throwType): ObjectType {
6464
return new ObjectType($throwType);
6565
}, $throwTypes)
6666
);

src/DynamicThrowTypeService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function getMethodThrowType(MethodReflection $methodReflection, MethodCal
127127
$throwType = $methodReflection->getThrowType();
128128
}
129129

130-
return $throwType !== null ? $throwType : new VoidType();
130+
return $throwType ?? new VoidType();
131131
}
132132

133133
public function getStaticMethodThrowType(MethodReflection $methodReflection, StaticCall $staticCall, Scope $scope): Type
@@ -159,7 +159,7 @@ public function getStaticMethodThrowType(MethodReflection $methodReflection, Sta
159159
$throwType = $methodReflection->getThrowType();
160160
}
161161

162-
return $throwType !== null ? $throwType : new VoidType();
162+
return $throwType ?? new VoidType();
163163
}
164164

165165
public function getConstructorThrowType(MethodReflection $methodReflection, New_ $newNode, Scope $scope): Type
@@ -189,7 +189,7 @@ public function getConstructorThrowType(MethodReflection $methodReflection, New_
189189
$throwType = $methodReflection->getThrowType();
190190
}
191191

192-
return $throwType !== null ? $throwType : new VoidType();
192+
return $throwType ?? new VoidType();
193193
}
194194

195195
public function getFunctionThrowType(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
@@ -210,7 +210,7 @@ public function getFunctionThrowType(FunctionReflection $functionReflection, Fun
210210

211211
$throwType = $functionReflection->getThrowType();
212212

213-
return $throwType !== null ? $throwType : new VoidType();
213+
return $throwType ?? new VoidType();
214214
}
215215

216216
}

src/Rules/ThrowsPhpDocRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private function processThrow(Throw_ $node, Scope $scope): array
181181
$exceptionClassNames = $this->throwsScope->filterExceptionsByUncaught($exceptionClassNames);
182182
$exceptionClassNames = $this->checkedExceptionService->filterCheckedExceptions($exceptionClassNames);
183183

184-
return array_map(function (string $exceptionClassName): string {
184+
return array_map(static function (string $exceptionClassName): string {
185185
return sprintf('Missing @throws %s annotation', $exceptionClassName);
186186
}, $exceptionClassNames);
187187
}
@@ -515,7 +515,7 @@ private function processThrowsTypes(Type $targetThrowType): array
515515
$targetExceptionClasses = $this->throwsScope->filterExceptionsByUncaught($targetExceptionClasses);
516516
$targetExceptionClasses = $this->checkedExceptionService->filterCheckedExceptions($targetExceptionClasses);
517517

518-
return array_map(function (string $targetExceptionClass): string {
518+
return array_map(static function (string $targetExceptionClass): string {
519519
return sprintf('Missing @throws %s annotation', $targetExceptionClass);
520520
}, $targetExceptionClasses);
521521
}

src/Rules/UnreachableCatchRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function processNode(Node $node, Scope $scope): array
4343
/** @var string[] $caughtClasses */
4444
$caughtClasses = [];
4545
foreach ($node->catches as $catch) {
46-
$catchClasses = array_map(function (Name $node): string {
46+
$catchClasses = array_map(static function (Name $node): string {
4747
return $node->toString();
4848
}, $catch->types);
4949

tests/src/RuleTestCase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ public function analyse(string $file): void
109109
$expectedErrors = $this->parseExpectedErrors($file);
110110
$actualErrors = $this->getAnalyser()->analyse([$file], false);
111111

112-
$strictlyTypedSprintf = function (int $line, string $message): string {
112+
$strictlyTypedSprintf = static function (int $line, string $message): string {
113113
return sprintf('%02d: %s', $line, $message);
114114
};
115115

116116
$expectedErrors = array_map(
117-
function (array $error) use ($strictlyTypedSprintf): string {
117+
static function (array $error) use ($strictlyTypedSprintf): string {
118118
if (!isset($error[0])) {
119119
throw new InvalidArgumentException('Missing expected error message.');
120120
}
@@ -127,7 +127,7 @@ function (array $error) use ($strictlyTypedSprintf): string {
127127
);
128128

129129
$actualErrors = array_map(
130-
function (Error $error): string {
130+
static function (Error $error): string {
131131
return sprintf('%02d: %s', $error->getLine(), $error->getMessage());
132132
},
133133
$actualErrors

tests/src/Rules/ThrowsPhpDocInheritanceRuleTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ThrowsPhpDocInheritanceRuleTest extends RuleTestCase
1313

1414
protected function getRule(): Rule
1515
{
16-
$throwsRule = new ThrowsPhpDocInheritanceRule(
16+
return new ThrowsPhpDocInheritanceRule(
1717
new CheckedExceptionService(
1818
[
1919
RuntimeException::class,
@@ -22,8 +22,6 @@ protected function getRule(): Rule
2222
self::getContainer()->getByType(FileTypeMapper::class),
2323
$this->createBroker()
2424
);
25-
26-
return $throwsRule;
2725
}
2826

2927
public function testInheritance(): void

tests/src/Rules/ThrowsPhpDocRuleTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ThrowsPhpDocRuleTest extends RuleTestCase
2121
protected function getRule(): Rule
2222
{
2323
$dynamicExtension = new DynamicExtension();
24-
$throwsRule = new ThrowsPhpDocRule(
24+
return new ThrowsPhpDocRule(
2525
new CheckedExceptionService(
2626
[
2727
RuntimeException::class,
@@ -40,8 +40,6 @@ protected function getRule(): Rule
4040
$this->createBroker(),
4141
$this->reportUnusedCatchesOfUncheckedExceptions
4242
);
43-
44-
return $throwsRule;
4543
}
4644

4745
public function testBasicThrows(): void

0 commit comments

Comments
 (0)