Browse Source

DX: use modernize_strpos

Dariusz Ruminski 3 years ago
parent
commit
5b040122f3

+ 1 - 0
.php-cs-fixer.dist.php

@@ -43,6 +43,7 @@ $config
         '@PhpCsFixer:risky' => true,
         'general_phpdoc_annotation_remove' => ['annotations' => ['expectedDeprecation']], // one should use PHPUnit built-in method instead
         'header_comment' => ['header' => $header],
+        'modernize_strpos' => true, // needs PHP 8+ or polyfill
     ])
     ->setFinder($finder)
 ;

+ 1 - 0
composer.json

@@ -27,6 +27,7 @@
         "symfony/finder": "^4.4.20 || ^5.0",
         "symfony/options-resolver": "^4.4.20 || ^5.0",
         "symfony/polyfill-php72": "^1.23",
+        "symfony/polyfill-php80": "^1.23",
         "symfony/polyfill-php81": "^1.23",
         "symfony/process": "^4.4.20 || ^5.0",
         "symfony/stopwatch": "^4.4.20 || ^5.0"

+ 1 - 1
src/AbstractLinesBeforeNamespaceFixer.php

@@ -48,7 +48,7 @@ abstract class AbstractLinesBeforeNamespaceFixer extends AbstractFixer implement
                 if ($token->isGivenKind(T_OPEN_TAG)) {
                     $openingToken = $token;
                     $openingTokenIndex = $index - $i;
-                    $newlineInOpening = false !== strpos($token->getContent(), "\n");
+                    $newlineInOpening = str_contains($token->getContent(), "\n");
                     if ($newlineInOpening) {
                         ++$precedingNewlines;
                     }

+ 1 - 1
src/Console/ConfigurationResolver.php

@@ -881,7 +881,7 @@ final class ConfigurationResolver
                     }
 
                     foreach ($pathsByType['dir'] as $path) {
-                        if (0 === strpos($currentRealPath, $path)) {
+                        if (str_starts_with($currentRealPath, $path)) {
                             return true;
                         }
                     }

+ 2 - 2
src/DocBlock/Line.php

@@ -81,7 +81,7 @@ final class Line
      */
     public function isTheStart(): bool
     {
-        return false !== strpos($this->content, '/**');
+        return str_contains($this->content, '/**');
     }
 
     /**
@@ -89,7 +89,7 @@ final class Line
      */
     public function isTheEnd(): bool
     {
-        return false !== strpos($this->content, '*/');
+        return str_contains($this->content, '*/');
     }
 
     /**

+ 1 - 1
src/DocBlock/TypeExpression.php

@@ -261,7 +261,7 @@ final class TypeExpression
             return $matches[1];
         }
 
-        if (0 === strpos($type, '\\')) {
+        if (str_starts_with($type, '\\')) {
             return substr($type, 1);
         }
 

+ 1 - 1
src/Fixer/Alias/EregToPregFixer.php

@@ -164,7 +164,7 @@ final class EregToPregFixer extends AbstractFixer
         // try do find something that's not used
         $delimiters = [];
         foreach (self::$delimiters as $k => $d) {
-            if (false === strpos($pattern, $d)) {
+            if (!str_contains($pattern, $d)) {
                 return $d;
             }
 

+ 7 - 7
src/Fixer/Basic/BracesFixer.php

@@ -237,7 +237,7 @@ class Foo
             $commentIndex = $tokens->getNextNonWhitespace($prevIndex);
             $commentToken = $tokens[$commentIndex];
 
-            if (!$commentToken->isGivenKind(T_COMMENT) || 0 === strpos($commentToken->getContent(), '/*')) {
+            if (!$commentToken->isGivenKind(T_COMMENT) || str_starts_with($commentToken->getContent(), '/*')) {
                 continue;
             }
 
@@ -599,7 +599,7 @@ class Foo
                 if (
                     !$isAnonymousClass
                     && $tokens[$closingParenthesisIndex - 1]->isWhitespace()
-                    && false !== strpos($tokens[$closingParenthesisIndex - 1]->getContent(), "\n")
+                    && str_contains($tokens[$closingParenthesisIndex - 1]->getContent(), "\n")
                 ) {
                     if (!$tokens[$startBraceIndex - 2]->isComment()) {
                         $tokens->ensureWhitespaceAtIndex($startBraceIndex - 1, 1, ' ');
@@ -888,8 +888,8 @@ class Foo
                 $previousToken->isWhitespace()
                 && 1 === Preg::match('/\R$/', $previousToken->getContent())
                 && (
-                    (0 === strpos($nextTokenContent, '//'.$this->whitespacesConfig->getIndent()) || '//' === $nextTokenContent)
-                    || (0 === strpos($nextTokenContent, '#'.$this->whitespacesConfig->getIndent()) || '#' === $nextTokenContent)
+                    (str_starts_with($nextTokenContent, '//'.$this->whitespacesConfig->getIndent()) || '//' === $nextTokenContent)
+                    || (str_starts_with($nextTokenContent, '#'.$this->whitespacesConfig->getIndent()) || '#' === $nextTokenContent)
                 )
             ) {
                 return;
@@ -911,7 +911,7 @@ class Foo
     private function isMultilined(Tokens $tokens, int $startParenthesisIndex, int $endParenthesisIndex): bool
     {
         for ($i = $startParenthesisIndex; $i < $endParenthesisIndex; ++$i) {
-            if (false !== strpos($tokens[$i]->getContent(), "\n")) {
+            if (str_contains($tokens[$i]->getContent(), "\n")) {
                 return true;
             }
         }
@@ -933,7 +933,7 @@ class Foo
             return false;
         }
 
-        if (0 === strpos($tokens[$index]->getContent(), '/*')) {
+        if (str_starts_with($tokens[$index]->getContent(), '/*')) {
             return true;
         }
 
@@ -979,7 +979,7 @@ class Foo
             if (null === $siblingIndex) {
                 return null;
             }
-        } while (0 === strpos($tokens[$siblingIndex]->getContent(), '/*'));
+        } while (str_starts_with($tokens[$siblingIndex]->getContent(), '/*'));
 
         $newLines = 0;
         for ($i = min($siblingIndex, $index) + 1, $max = max($siblingIndex, $index); $i < $max; ++$i) {

+ 1 - 1
src/Fixer/Basic/NonPrintableCharacterFixer.php

@@ -139,7 +139,7 @@ final class NonPrintableCharacterFixer extends AbstractFixer implements Configur
                 if ($previousToken->isGivenKind(T_START_HEREDOC)) {
                     $previousTokenContent = $previousToken->getContent();
 
-                    if (false !== strpos($previousTokenContent, '\'')) {
+                    if (str_contains($previousTokenContent, '\'')) {
                         $tokens[$index - 1] = new Token([T_START_HEREDOC, str_replace('\'', '', $previousTokenContent)]);
                         $stringTypeChanged = true;
                     }

+ 1 - 1
src/Fixer/Basic/PsrAutoloadingFixer.php

@@ -153,7 +153,7 @@ class InvalidName {}
      */
     protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
     {
-        if (null !== $this->configuration['dir'] && 0 !== strpos($file->getRealPath(), $this->configuration['dir'])) {
+        if (null !== $this->configuration['dir'] && !str_starts_with($file->getRealPath(), $this->configuration['dir'])) {
             return;
         }
 

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