Browse Source

DX: use booleans in conditions (#7149)

Kuba Werłos 1 year ago
parent
commit
e25cee6a00

+ 0 - 140
dev-tools/phpstan/baseline.php

@@ -16,16 +16,6 @@ $ignoreErrors[] = [
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Console/ConfigurationResolver.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a ternary operator condition, string given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Console/Report/FixReport/ReporterFactory.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a ternary operator condition, string given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Console/Report/ListSetsReport/ReporterFactory.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Foreach overwrites \\$token with its value variable\\.$#',
 	'count' => 1,
@@ -36,31 +26,6 @@ $ignoreErrors[] = [
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Doctrine/Annotation/Tokens.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in &&, string given on the left side\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Documentation/FixerDocumentGenerator.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in &&, string given on the right side\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Documentation/FixerDocumentGenerator.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in \\|\\|, string given on the left side\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Documentation/FixerDocumentGenerator.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in \\|\\|, string given on the right side\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Documentation/FixerDocumentGenerator.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a ternary operator condition, array\\<string, int\\|string\\>\\|null given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/FileReader.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Parameter \\#1 \\$configuration \\(array\\<string, mixed\\>\\) of method PhpCsFixer\\\\Fixer\\\\Alias\\\\NoAliasFunctionsFixer\\:\\:configure\\(\\) should be contravariant with parameter \\$configuration \\(array\\) of method PhpCsFixer\\\\Fixer\\\\ConfigurableFixerInterface\\:\\:configure\\(\\)$#',
 	'count' => 1,
@@ -206,11 +171,6 @@ $ignoreErrors[] = [
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Fixer/ClassNotation/ClassDefinitionFixer.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a ternary operator condition, int\\<0, max\\> given\\.$#',
-	'count' => 2,
-	'path' => __DIR__ . '/../../src/Fixer/ClassNotation/ClassDefinitionFixer.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Only booleans are allowed in &&, int\\|null given on the right side\\.$#',
 	'count' => 1,
@@ -221,11 +181,6 @@ $ignoreErrors[] = [
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Fixer/ClassNotation/FinalInternalClassFixer.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in an if condition, string given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Fixer/ClassNotation/FinalInternalClassFixer.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Parameter \\#1 \\$configuration \\(array\\<string, mixed\\>\\) of method PhpCsFixer\\\\Fixer\\\\ClassNotation\\\\FinalInternalClassFixer\\:\\:configure\\(\\) should be contravariant with parameter \\$configuration \\(array\\) of method PhpCsFixer\\\\Fixer\\\\ConfigurableFixerInterface\\:\\:configure\\(\\)$#',
 	'count' => 1,
@@ -281,11 +236,6 @@ $ignoreErrors[] = [
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Fixer/Comment/CommentToPhpdocFixer.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in an if condition, int given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Fixer/Comment/HeaderCommentFixer.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Method PhpCsFixer\\\\Fixer\\\\Comment\\\\NoEmptyCommentFixer\\:\\:getCommentBlock\\(\\) return type has no value type specified in iterable type array\\.$#',
 	'count' => 1,
@@ -311,31 +261,11 @@ $ignoreErrors[] = [
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Fixer/ConstantNotation/NativeConstantInvocationFixer.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in &&, PhpCsFixer\\\\Tokenizer\\\\Token\\|null given on the left side\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Fixer/ControlStructure/ControlStructureBracesFixer.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a negated boolean, PhpCsFixer\\\\Tokenizer\\\\Token given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Fixer/ControlStructure/ControlStructureBracesFixer.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in &&, int given on the left side\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Fixer/ControlStructure/IncludeFixer.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Method PhpCsFixer\\\\Fixer\\\\ControlStructure\\\\YodaStyleFixer\\:\\:getCompareFixableInfo\\(\\) return type has no value type specified in iterable type array\\.$#',
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Fixer/ControlStructure/YodaStyleFixer.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a negated boolean, int given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Fixer/ControlStructure/YodaStyleFixer.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Parameter \\#1 \\$configuration \\(array\\<string, mixed\\>\\) of method PhpCsFixer\\\\Fixer\\\\ControlStructure\\\\YodaStyleFixer\\:\\:configure\\(\\) should be contravariant with parameter \\$configuration \\(array\\) of method PhpCsFixer\\\\Fixer\\\\ConfigurableFixerInterface\\:\\:configure\\(\\)$#',
 	'count' => 1,
@@ -606,26 +536,11 @@ $ignoreErrors[] = [
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixer.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a ternary operator condition, int\\|null given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Fixer/Phpdoc/PhpdocAddMissingParamAnnotationFixer.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Fixer/Phpdoc/PhpdocAddMissingParamAnnotationFixer.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a ternary operator condition, string\\|null given\\.$#',
-	'count' => 2,
-	'path' => __DIR__ . '/../../src/Fixer/Phpdoc/PhpdocAlignFixer.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in \\|\\|, string\\|null given on the right side\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Fixer/Phpdoc/PhpdocAlignFixer.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Parameter \\#1 \\$configuration \\(array\\<string, mixed\\>\\) of method PhpCsFixer\\\\Fixer\\\\Phpdoc\\\\PhpdocAlignFixer\\:\\:configure\\(\\) should be contravariant with parameter \\$configuration \\(array\\) of method PhpCsFixer\\\\Fixer\\\\ConfigurableFixerInterface\\:\\:configure\\(\\)$#',
 	'count' => 1,
@@ -641,11 +556,6 @@ $ignoreErrors[] = [
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Fixer/Phpdoc/PhpdocNoAliasTagFixer.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in an if condition, int\\<0, max\\> given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Fixer/Phpdoc/PhpdocParamOrderFixer.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^For loop initial assignment overwrites variable \\$pos\\.$#',
 	'count' => 2,
@@ -726,26 +636,11 @@ $ignoreErrors[] = [
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Fixer/Whitespace/TypesSpacesFixer.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a ternary operator condition, string given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/FixerFactory.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Linter/ProcessLintingResult.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a ternary operator condition, array\\<string, array\\<int, string\\>\\|string\\>\\|null given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Runner/Runner.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a ternary operator condition, array\\<string, int\\|string\\>\\|null given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Runner/Runner.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',
 	'count' => 1,
@@ -766,11 +661,6 @@ $ignoreErrors[] = [
 	'count' => 2,
 	'path' => __DIR__ . '/../../src/Tokenizer/Analyzer/Analysis/ArgumentAnalysis.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a ternary operator condition, string\\|null given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Tokenizer/Analyzer/ArgumentsAnalyzer.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^For loop initial assignment overwrites variable \\$index\\.$#',
 	'count' => 1,
@@ -821,41 +711,11 @@ $ignoreErrors[] = [
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Tokenizer/Transformer/ConstructorPromotionTransformer.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a ternary operator condition, string given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Tokenizer/Transformers.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a ternary operator condition, string given\\.$#',
-	'count' => 2,
-	'path' => __DIR__ . '/../../tests/AutoReview/ProjectCodeTest.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a ternary operator condition, int given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../tests/DocBlock/TypeExpressionTest.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a ternary operator condition, int\\<0, 1\\> given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../tests/DocBlock/TypeExpressionTest.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Variable \\$expression might not be defined\\.$#',
 	'count' => 1,
 	'path' => __DIR__ . '/../../tests/DocBlock/TypeExpressionTest.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a ternary operator condition, SplFileInfo\\|null given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../tests/Fixer/Basic/EncodingFixerTest.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Only booleans are allowed in a ternary operator condition, string\\|null given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../tests/Fixer/Basic/EncodingFixerTest.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Only booleans are allowed in a negated boolean, string\\|null given\\.$#',
 	'count' => 1,

+ 1 - 1
src/Console/Report/FixReport/ReporterFactory.php

@@ -39,7 +39,7 @@ final class ReporterFactory
                 $builtInReporters[] = sprintf(
                     '%s\\%s%s',
                     __NAMESPACE__,
-                    $relativeNamespace ? $relativeNamespace.'\\' : '',
+                    '' !== $relativeNamespace ? $relativeNamespace.'\\' : '',
                     $file->getBasename('.php')
                 );
             }

+ 1 - 1
src/Console/Report/ListSetsReport/ReporterFactory.php

@@ -41,7 +41,7 @@ final class ReporterFactory
                 $builtInReporters[] = sprintf(
                     '%s\\%s%s',
                     __NAMESPACE__,
-                    $relativeNamespace ? $relativeNamespace.'\\' : '',
+                    '' !== $relativeNamespace ? $relativeNamespace.'\\' : '',
                     $file->getBasename('.php')
                 );
             }

+ 2 - 2
src/Documentation/FixerDocumentGenerator.php

@@ -103,10 +103,10 @@ Using this rule is risky
 RST;
         }
 
-        if ($deprecationDescription || $riskyDescription) {
+        if ('' !== $deprecationDescription || '' !== $riskyDescription) {
             $warningsHeader = 'Warning';
 
-            if ($deprecationDescription && $riskyDescription) {
+            if ('' !== $deprecationDescription && '' !== $riskyDescription) {
                 $warningsHeader = 'Warnings';
             }
 

+ 1 - 1
src/FileReader.php

@@ -64,7 +64,7 @@ final class FileReader
             throw new \RuntimeException(sprintf(
                 'Failed to read content from "%s".%s',
                 $realPath,
-                $error ? ' '.$error['message'] : ''
+                null !== $error ? ' '.$error['message'] : ''
             ));
         }
 

+ 2 - 2
src/Fixer/ClassNotation/ClassDefinitionFixer.php

@@ -306,11 +306,11 @@ $foo = new class(){};
 
         if (!$tokens[$classyIndex]->isGivenKind(T_TRAIT)) {
             $extends = $tokens->findGivenKind(T_EXTENDS, $classyIndex, $openIndex);
-            $def['extends'] = \count($extends) ? $this->getClassyInheritanceInfo($tokens, key($extends), 'numberOfExtends') : false;
+            $def['extends'] = [] !== $extends ? $this->getClassyInheritanceInfo($tokens, key($extends), 'numberOfExtends') : false;
 
             if (!$tokens[$classyIndex]->isGivenKind(T_INTERFACE)) {
                 $implements = $tokens->findGivenKind(T_IMPLEMENTS, $classyIndex, $openIndex);
-                $def['implements'] = \count($implements) ? $this->getClassyInheritanceInfo($tokens, key($implements), 'numberOfImplements') : false;
+                $def['implements'] = [] !== $implements ? $this->getClassyInheritanceInfo($tokens, key($implements), 'numberOfImplements') : false;
                 $def['anonymousClass'] = $tokensAnalyzer->isAnonymousClass($classyIndex);
             }
         }

+ 1 - 1
src/Fixer/ClassNotation/FinalInternalClassFixer.php

@@ -289,7 +289,7 @@ final class FinalInternalClassFixer extends AbstractFixer implements Configurabl
 
         while ($currentIndex < $endIndex && $currentIndex = $tokens->getNextMeaningfulToken($currentIndex)) {
             if (!$tokens[$currentIndex]->isGivenKind([T_STRING, T_NS_SEPARATOR])) {
-                if ($attributeString) {
+                if ('' !== $attributeString) {
                     $attributeCandidates[$attributeString] = true;
                     $attributeString = '';
                 }

+ 1 - 1
src/Fixer/Comment/HeaderCommentFixer.php

@@ -163,7 +163,7 @@ echo 1;
             $expectedLocation = $possibleLocation === $location;
 
             if (!$sameComment || !$expectedLocation) {
-                if ($expectedLocation ^ $sameComment) {
+                if ($expectedLocation xor $sameComment) {
                     $this->removeHeader($tokens, $headerCurrentIndex);
                 }
 

+ 5 - 6
src/Fixer/ControlStructure/ControlStructureBracesFixer.php

@@ -130,12 +130,12 @@ final class ControlStructureBracesFixer extends AbstractFixer
     private function findStatementEnd(Tokens $tokens, int $parenthesisEndIndex): int
     {
         $nextIndex = $tokens->getNextMeaningfulToken($parenthesisEndIndex);
-        $nextToken = $tokens[$nextIndex];
-
-        if (!$nextToken) {
+        if (null === $nextIndex) {
             return $parenthesisEndIndex;
         }
 
+        $nextToken = $tokens[$nextIndex];
+
         if ($nextToken->equals('{')) {
             return $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $nextIndex);
         }
@@ -150,13 +150,12 @@ final class ControlStructureBracesFixer extends AbstractFixer
 
                 while (true) {
                     $nextIndex = $tokens->getNextMeaningfulToken($endIndex);
-                    $nextToken = isset($nextIndex) ? $tokens[$nextIndex] : null;
-                    if ($nextToken && $nextToken->isGivenKind($this->getControlContinuationTokensForOpeningToken($openingTokenKind))) {
+                    if (null !== $nextIndex && $tokens[$nextIndex]->isGivenKind($this->getControlContinuationTokensForOpeningToken($openingTokenKind))) {
                         $parenthesisEndIndex = $this->findParenthesisEnd($tokens, $nextIndex);
 
                         $endIndex = $this->findStatementEnd($tokens, $parenthesisEndIndex);
 
-                        if ($nextToken->isGivenKind($this->getFinalControlContinuationTokensForOpeningToken($openingTokenKind))) {
+                        if ($tokens[$nextIndex]->isGivenKind($this->getFinalControlContinuationTokensForOpeningToken($openingTokenKind))) {
                             return $endIndex;
                         }
                     } else {

+ 1 - 1
src/Fixer/ControlStructure/IncludeFixer.php

@@ -64,7 +64,7 @@ include_once("sample4.php");
         $blocksAnalyzer = new BlocksAnalyzer();
 
         foreach ($includies as $includy) {
-            if ($includy['end'] && !$tokens[$includy['end']]->isGivenKind(T_CLOSE_TAG)) {
+            if (!$tokens[$includy['end']]->isGivenKind(T_CLOSE_TAG)) {
                 $afterEndIndex = $tokens->getNextNonWhitespace($includy['end']);
 
                 if (null === $afterEndIndex || !$tokens[$afterEndIndex]->isComment()) {

Some files were not shown because too many files changed in this diff