Skip to content

Update ruleset for modern (PHP 8+) code #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public function register(): array
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
*/
#[\Override]
public function process(File $phpcsFile, $stackPtr): void
Expand Down
1 change: 0 additions & 1 deletion MO4/Sniffs/Arrays/MultiLineArraySniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function register(): array
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
*/
#[\Override]
public function process(File $phpcsFile, $stackPtr): void
Expand Down
2 changes: 0 additions & 2 deletions MO4/Sniffs/Commenting/PropertyCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public function __construct()
* opened the scope that this test is
* listening for.
*
* @return void
*
* @throws RuntimeException
*/
Expand Down Expand Up @@ -244,7 +243,6 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
* @param int $stackPtr The position in the stack where this
* token was found.
*
* @return void
*/
#[\Override]
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr): void
Expand Down
9 changes: 1 addition & 8 deletions MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ class AlphabeticalUseStatementsSniff extends UseDeclarationSniff
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
*/
#[\Override]
public function process(File $phpcsFile, $stackPtr): void
Expand Down Expand Up @@ -172,7 +171,7 @@ public function process(File $phpcsFile, $stackPtr): void
* @param File $phpcsFile PHP CS File
* @param int $stackPtr pointer
*
* @return array|false
* @return array{startPtr: int, content: string}|false
*/
private function getUseImport(File $phpcsFile, int $stackPtr)
{
Expand Down Expand Up @@ -208,7 +207,6 @@ private function getUseImport(File $phpcsFile, int $stackPtr)
* @param File $phpcsFile PHP CS File
* @param int $stackPtr pointer
*
* @return string
*/
private function getUseStatementAsString(File $phpcsFile, int $stackPtr): string
{
Expand All @@ -231,7 +229,6 @@ private function getUseStatementAsString(File $phpcsFile, int $stackPtr): string
* @param File $phpcsFile PHP CS File
* @param int $stackPtr pointer
*
* @return bool
*/
private function checkIsNonImportUse(File $phpcsFile, int $stackPtr): bool
{
Expand Down Expand Up @@ -265,7 +262,6 @@ private function checkIsNonImportUse(File $phpcsFile, int $stackPtr): bool
* @param File $phpcsFile PHP CS file
* @param int $stackPtr pointer
*
* @return void
*/
private function fixerClearLine(File $phpcsFile, int $stackPtr): void
{
Expand All @@ -289,7 +285,6 @@ private function fixerClearLine(File $phpcsFile, int $stackPtr): void
* @param int $stackPtr pointer
* @param string $import import string requiring new position
*
* @return int
*/
private function findNewDestination(File $phpcsFile, int $stackPtr, string $import): int
{
Expand Down Expand Up @@ -332,7 +327,6 @@ private function findNewDestination(File $phpcsFile, int $stackPtr, string $impo
* @param string $a first namespace string
* @param string $b second namespace string
*
* @return int
*/
private function compareString(string $a, string $b): int
{
Expand All @@ -357,7 +351,6 @@ private function compareString(string $a, string $b): int
* @param string $a first namespace string
* @param string $b second namespace string
*
* @return int
*/
private function dictionaryCompare(string $a, string $b): int
{
Expand Down
20 changes: 8 additions & 12 deletions MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public function register(): array
* file's token stack where the token
* was found.
*
* @return void
*
* @throws RuntimeException
*/
Expand Down Expand Up @@ -212,7 +211,7 @@ public function process(File $phpcsFile, $stackPtr): void
* @param int $start start pointer
* @param int $end end pointer
*
* @return array
* @return array<string, string>
*/
protected function getUseStatements(File $phpcsFile, int $start, int $end): array
{
Expand Down Expand Up @@ -281,7 +280,6 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
* @param int $start start pointer
* @param int $end end pointer
*
* @return string
*/
protected function getNamespace(File $phpcsFile, int $start, int $end): string
{
Expand Down Expand Up @@ -315,7 +313,6 @@ protected function getNamespace(File $phpcsFile, int $start, int $end): string
*
* @param string $className class name
*
* @return string
*/
private function getFullyQualifiedClassName(string $className): string
{
Expand All @@ -325,15 +322,14 @@ private function getFullyQualifiedClassName(string $className): string
/**
* Check if short hand is possible.
*
* @param File $phpcsFile PHP CS File
* @param array $useStatements array with class use statements
* @param string $className class name
* @param string $namespace name space
* @param int $startPtr start token pointer
* @param int $endPtr end token pointer
* @param bool $isDocBlock true if fixing doc block
* @param File $phpcsFile PHP CS File
* @param array<string, string> $useStatements array with class use statements
* @param string $className class name
* @param string $namespace name space
* @param int $startPtr start token pointer
* @param int $endPtr end token pointer
* @param bool $isDocBlock true if fixing doc block
*
* @return void
*/
private function checkShorthandPossible(File $phpcsFile, array $useStatements, string $className, string $namespace, int $startPtr, int $endPtr, bool $isDocBlock = false): void
{
Expand Down
3 changes: 0 additions & 3 deletions MO4/Sniffs/Strings/VariableInDoubleQuotedStringSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function register(): array
* file's token stack where the token
* was found.
*
* @return void
*/
#[\Override]
public function process(File $phpcsFile, $stackPtr): void
Expand Down Expand Up @@ -130,7 +129,6 @@ public function process(File $phpcsFile, $stackPtr): void
* @param int $pos position
* @param string $var variable
*
* @return string
*/
private function surroundVariableWithBraces(string $content, int $pos, string $var): string
{
Expand All @@ -147,7 +145,6 @@ private function surroundVariableWithBraces(string $content, int $pos, string $v
* @param string $correctVariable correct variable
* @param File $phpCsFile PHP_CodeSniffer File object
*
* @return void
*/
private function fixPhpCsFile(int $stackPtr, string $correctVariable, File $phpCsFile): void
{
Expand Down
1 change: 0 additions & 1 deletion MO4/Sniffs/WhiteSpace/ConstantSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public function register(): array
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
*/
#[\Override]
public function process(File $phpcsFile, $stackPtr): void
Expand Down
2 changes: 1 addition & 1 deletion MO4/Sniffs/WhiteSpace/MultipleEmptyLinesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function process(File $phpcsFile, $stackPtr)
$next = $stackPtr + 1;

while (isset($tokens[$next + 1]) &&
$tokens[$next]['code'] === T_WHITESPACE &&
T_WHITESPACE === $tokens[$next]['code'] &&
$tokens[$next]['line'] !== $tokens[$next + 1]['line']
) {
$next++;
Expand Down
4 changes: 2 additions & 2 deletions MO4/Tests/AbstractMo4SniffUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ protected function getWarningList(string $testFile = ''): array
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @param string $testFile test file
* @param array $list record for test file
* @param string $testFile test file
* @param array<array<int>> $list record for test file
*
* @return array<int, int>
*
Expand Down
49 changes: 38 additions & 11 deletions MO4/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<!-- exclude sniffs that are disabled on purpose by this standard -->
<exclude name="Symfony.Commenting.ClassComment.Missing"/>
<exclude name="Symfony.Commenting.License"/>
<!-- SlevomatCodingStandard.ControlStructures.RequireYodaComparison supports autofixing -->
<exclude name="Symfony.ControlStructure.YodaConditions.Invalid"/>
<!-- disable forcing of function type hints as we're requiring native types -->
<exclude name="Symfony.Commenting.FunctionComment"/>
</rule>

<!-- Also include PSR12 -->
Expand Down Expand Up @@ -131,8 +135,16 @@
</rule>
<!-- Require usage of ::class instead of __CLASS__, get_class(), get_class($this), get_called_class() and get_parent_class() -->
<rule ref="SlevomatCodingStandard.Classes.ModernClassNameReference"/>
<!-- Require use of constructor property promotion -->
<rule ref="SlevomatCodingStandard.Classes.RequireConstructorPropertyPromotion"/>
<!-- Forbid static:: in final class -->
<rule ref="SlevomatCodingStandard.Classes.UselessLateStaticBinding"/>
<!-- Forbid empty comments-->
<rule ref="SlevomatCodingStandard.Commenting.EmptyComment"/>
<!-- report invalid format of inline phpDocs with @var -->
<rule ref="SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration"/>
<!-- Forbid useless function doc comments -->
<rule ref="SlevomatCodingStandard.Commenting.UselessFunctionDocComment"/>
<!-- Forbid useless @inheritDoc comments -->
<rule ref="SlevomatCodingStandard.Commenting.UselessInheritDocComment"/>
<!-- Forbid assignments in conditions -->
Expand All @@ -159,13 +171,21 @@
<rule ref="SlevomatCodingStandard.ControlStructures.NewWithParentheses"/>
<!-- Require usage of null coalesce operator when possible -->
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator"/>
<!-- Require usage of null coalesce equal operator when possible -->
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceEqualOperator"/>
<!-- Require usage of null safe operator when possible -->
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullSafeObjectOperator"/>
<!-- Require usage of ternary operator when possible -->
<rule ref="SlevomatCodingStandard.ControlStructures.RequireTernaryOperator">
<!-- But multiline is useful for readability -->
<properties>
<property name="ignoreMultiLine" value="true"/>
</properties>
</rule>
<!-- Require Yoda comparisons -->
<rule ref="SlevomatCodingStandard.ControlStructures.RequireYodaComparison"/>
<!-- Formatting of arrow functions -->
<rule ref="SlevomatCodingStandard.Functions.ArrowFunctionDeclaration"/>
<!-- Enforce consistent spacing of named arguments -->
<rule ref="SlevomatCodingStandard.Functions.NamedArgumentSpacing"/>
<!-- Require static closures for microoptimization -->
Expand Down Expand Up @@ -229,19 +249,15 @@
<!-- Require one space after and no space before colon in return types -->
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing"/>
<!-- Require parameter type hints to be declared -->
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint">
<properties>
<property name="enableMixedTypeHint" value="false"/>
<property name="enableUnionTypeHint" value="false"/>
<property name="enableIntersectionTypeHint" value="false"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint"/>
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.UselessAnnotation">
<severity>0</severity>
<severity>9</severity>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification">
<severity>0</severity>
<severity>9</severity>
</rule>
<!-- check spacing of parameter type hints -->
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHintSpacing"/>
<!-- Require return type hints to be declared -->
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint">
<properties>
Expand All @@ -251,10 +267,21 @@
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation">
<severity>0</severity>
<severity>9</severity>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification">
<severity>0</severity>
<severity>9</severity>
</rule>
<!-- check spacing of return type hints -->
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing"/>
<!-- DNF (union / intersection) type hint formatting -->
<rule ref="SlevomatCodingStandard.TypeHints.DNFTypeHintFormat">
<properties>
<property name="withSpacesAroundOperators" value="no"/>
<property name="withSpacesInsideParentheses" value="no"/>
<property name="shortNullable" value="yes"/>
<property name="nullPosition" value="last"/>
</properties>
</rule>
<!-- Forbid useless @var for constants -->
<rule ref="SlevomatCodingStandard.TypeHints.UselessConstantTypeHint"/>
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"php": "^8.1",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7 || ^1.0",
"escapestudios/symfony2-coding-standard": "^3.16.0",
"slevomat/coding-standard": "^8.14",
"slevomat/coding-standard": "^8.20",
"squizlabs/php_codesniffer": "^3.8.0"
},
"require-dev": {
Expand Down
10 changes: 4 additions & 6 deletions integrationtests/testfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public function __construct(string $dummy)
}

/**
* @return string
*
* @deprecated
*/
public function someDeprecatedMethod(): string
Expand All @@ -53,14 +51,14 @@ public function someDeprecatedMethod(): string
/**
* Transforms the input given as first argument.
*
* @param bool|string $dummy Some argument description
* @param array $options An options collection to be used within the transformation
* @param bool|string $dummy Some argument description
* @param array<string> $options An options collection to be used within the transformation
*
* @return string|null The transformed input
*
* @throws \RuntimeException When an invalid option is provided
*/
private function transformText($dummy, array $options = []): ?string
private function transformText(bool|string $dummy, array $options = []): ?string
{
/** @var array<string, string> $defaultOptions */
$defaultOptions = [
Expand Down Expand Up @@ -104,7 +102,7 @@ private function transformText($dummy, array $options = []): ?string
*
* @return bool|void The resultant check if $theSwitch isn't false, void otherwise
*/
private function reverseBoolean($value = null, bool $theSwitch = false)
private function reverseBoolean(mixed $value = null, bool $theSwitch = false)
{
if (!$theSwitch) {
return;
Expand Down
Loading