Browse Source

Add PregWrapper

Kuba Werłos 7 years ago
parent
commit
0391ce7017

+ 2 - 2
src/AbstractPsrAutoloadingFixer.php

@@ -64,7 +64,7 @@ abstract class AbstractPsrAutoloadingFixer extends AbstractFixer
             // ignore file with extension other than php
             (!isset($filenameParts[1]) || 'php' !== $filenameParts[1])
             // ignore file with name that cannot be a class name
-            || 0 === preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $filenameParts[0])
+            || 0 === Preg::match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $filenameParts[0])
             // ignore filename that will halt compiler (and cannot be properly tokenized under PHP 5.3)
             || '__halt_compiler' === $filenameParts[0]
         ) {
@@ -84,6 +84,6 @@ abstract class AbstractPsrAutoloadingFixer extends AbstractFixer
         }
 
         // ignore stubs/fixtures, since they are typically containing invalid files for various reasons
-        return !preg_match('{[/\\\\](stub|fixture)s?[/\\\\]}i', $file->getRealPath());
+        return !Preg::match('{[/\\\\](stub|fixture)s?[/\\\\]}i', $file->getRealPath());
     }
 }

+ 3 - 2
src/Console/Command/DescribeCommand.php

@@ -24,6 +24,7 @@ use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
 use PhpCsFixer\FixerDefinition\VersionSpecificCodeSampleInterface;
 use PhpCsFixer\FixerFactory;
+use PhpCsFixer\Preg;
 use PhpCsFixer\RuleSet;
 use PhpCsFixer\StdinFileInfo;
 use PhpCsFixer\Tokenizer\Tokens;
@@ -181,8 +182,8 @@ final class DescribeCommand extends Command
                     $line .= ' (<comment>'.implode('</comment>, <comment>', $allowed).'</comment>)';
                 }
 
-                $description = preg_replace('/(`.+?`)/', '<info>$1</info>', $option->getDescription());
-                $line .= ': '.lcfirst(preg_replace('/\.$/', '', $description)).'; ';
+                $description = Preg::replace('/(`.+?`)/', '<info>$1</info>', $option->getDescription());
+                $line .= ': '.lcfirst(Preg::replace('/\.$/', '', $description)).'; ';
                 if ($option->hasDefault()) {
                     $line .= sprintf(
                         'defaults to <comment>%s</comment>',

+ 11 - 10
src/Console/Command/HelpCommand.php

@@ -19,6 +19,7 @@ use PhpCsFixer\Fixer\DefinedFixerInterface;
 use PhpCsFixer\Fixer\FixerInterface;
 use PhpCsFixer\FixerConfiguration\FixerOptionInterface;
 use PhpCsFixer\FixerFactory;
+use PhpCsFixer\Preg;
 use PhpCsFixer\RuleSet;
 use Symfony\Component\Console\Command\HelpCommand as BaseHelpCommand;
 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
@@ -288,7 +289,7 @@ EOF
 
             $str = var_export($value, true);
             do {
-                $strNew = preg_replace(
+                $strNew = Preg::replace(
                     $replaces[0],
                     $replaces[1],
                     $str
@@ -304,7 +305,7 @@ EOF
             $str = var_export($value, true);
         }
 
-        return preg_replace('/\bNULL\b/', 'null', $str);
+        return Preg::replace('/\bNULL\b/', 'null', $str);
     }
 
     /**
@@ -370,7 +371,7 @@ EOF
         }
 
         for ($i = (int) Application::VERSION; $i > 0; --$i) {
-            if (1 === preg_match('/Changelog for v('.$i.'.\d+.\d+)/', $changelog, $matches)) {
+            if (1 === Preg::match('/Changelog for v('.$i.'.\d+.\d+)/', $changelog, $matches)) {
                 $version = $matches[1];
 
                 break;
@@ -447,7 +448,7 @@ EOF
             }
 
             $description = implode("\n   | ", self::wordwrap(
-                preg_replace('/(`.+?`)/', '<info>$1</info>', $description),
+                Preg::replace('/(`.+?`)/', '<info>$1</info>', $description),
                 72
             ));
 
@@ -460,10 +461,10 @@ EOF
             if ($fixer->isRisky()) {
                 $help .= sprintf(
                     "   | *Risky rule: %s.*\n",
-                    preg_replace(
+                    Preg::replace(
                         '/(`.+?`)/',
                         '<info>$1</info>',
-                        lcfirst(preg_replace('/\.$/', '', $fixer->getDefinition()->getRiskyDescription()))
+                        lcfirst(Preg::replace('/\.$/', '', $fixer->getDefinition()->getRiskyDescription()))
                     )
                 );
             }
@@ -497,10 +498,10 @@ EOF
                             $line .= ' (<comment>'.implode('</comment>, <comment>', $allowed).'</comment>)';
                         }
 
-                        $line .= ': '.preg_replace(
+                        $line .= ': '.Preg::replace(
                             '/(`.+?`)/',
                             '<info>$1</info>',
-                            lcfirst(preg_replace('/\.$/', '', $option->getDescription()))
+                            lcfirst(Preg::replace('/\.$/', '', $option->getDescription()))
                         ).'; ';
                         if ($option->hasDefault()) {
                             $line .= 'defaults to <comment>'.self::toString($option->getDefault()).'</comment>';
@@ -523,7 +524,7 @@ EOF
         }
 
         // prevent "\</foo>" from being rendered as an escaped literal style tag
-        return preg_replace('#\\\\(</.*?>)#', '<<$1', $help);
+        return Preg::replace('#\\\\(</.*?>)#', '<<$1', $help);
     }
 
     /**
@@ -540,7 +541,7 @@ EOF
         $currentLine = 0;
         $lineLength = 0;
         foreach (explode(' ', $string) as $word) {
-            $wordLength = strlen(preg_replace('~</?(\w+)>~', '', $word));
+            $wordLength = strlen(Preg::replace('~</?(\w+)>~', '', $word));
             if (0 !== $lineLength) {
                 ++$wordLength; // space before word
             }

+ 18 - 17
src/Console/Command/ReadmeCommand.php

@@ -12,6 +12,7 @@
 
 namespace PhpCsFixer\Console\Command;
 
+use PhpCsFixer\Preg;
 use Symfony\Component\Console\Command\Command;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
@@ -225,26 +226,26 @@ EOF;
         $help = $command->getHelp();
         $help = str_replace('%command.full_name%', 'php-cs-fixer.phar '.$command->getName(), $help);
         $help = str_replace('%command.name%', $command->getName(), $help);
-        $help = preg_replace('#</?(comment|info)>#', '``', $help);
-        $help = preg_replace('#`(``.+?``)`#', '$1', $help);
-        $help = preg_replace('#^(\s+)``(.+)``$#m', '$1$2', $help);
-        $help = preg_replace('#^ \* ``(.+)``(.*?\n)#m', "* **$1**$2\n", $help);
-        $help = preg_replace('#^   \\| #m', '  ', $help);
-        $help = preg_replace('#^   \\|#m', '', $help);
-        $help = preg_replace('#^(?=  \\*Risky rule: )#m', "\n", $help);
-        $help = preg_replace("#^(  Configuration options:\n)(  - )#m", "$1\n$2", $help);
-        $help = preg_replace("#^\n( +\\$ )#m", "\n.. code-block:: bash\n\n$1", $help);
-        $help = preg_replace("#^\n( +<\\?php)#m", "\n.. code-block:: php\n\n$1", $help);
-        $help = preg_replace_callback(
+        $help = Preg::replace('#</?(comment|info)>#', '``', $help);
+        $help = Preg::replace('#`(``.+?``)`#', '$1', $help);
+        $help = Preg::replace('#^(\s+)``(.+)``$#m', '$1$2', $help);
+        $help = Preg::replace('#^ \* ``(.+)``(.*?\n)#m', "* **$1**$2\n", $help);
+        $help = Preg::replace('#^   \\| #m', '  ', $help);
+        $help = Preg::replace('#^   \\|#m', '', $help);
+        $help = Preg::replace('#^(?=  \\*Risky rule: )#m', "\n", $help);
+        $help = Preg::replace("#^(  Configuration options:\n)(  - )#m", "$1\n$2", $help);
+        $help = Preg::replace("#^\n( +\\$ )#m", "\n.. code-block:: bash\n\n$1", $help);
+        $help = Preg::replace("#^\n( +<\\?php)#m", "\n.. code-block:: php\n\n$1", $help);
+        $help = Preg::replaceCallback(
             '#^\\s*<\\?(\\w+).*?\\?>#ms',
             function ($matches) {
-                $result = preg_replace("#^\\.\\. code-block:: bash\n\n#m", '', $matches[0]);
+                $result = Preg::replace("#^\\.\\. code-block:: bash\n\n#m", '', $matches[0]);
 
                 if ('php' !== $matches[1]) {
-                    $result = preg_replace("#<\\?{$matches[1]}\\s*#", '', $result);
+                    $result = Preg::replace("#<\\?{$matches[1]}\\s*#", '', $result);
                 }
 
-                $result = preg_replace("#\n\n +\\?>#", '', $result);
+                $result = Preg::replace("#\n\n +\\?>#", '', $result);
 
                 return $result;
             },
@@ -257,7 +258,7 @@ EOF;
         // Make to RST http://www.sphinx-doc.org/en/stable/rest.html#hyperlinks
         //      `description <http://...>`_
 
-        $help = preg_replace_callback(
+        $help = Preg::replaceCallback(
            '#`(.+)`\s?\(<url>(.+)<\/url>\)#',
             function (array $matches) {
                 return sprintf('`%s <%s>`_', str_replace('\\', '\\\\', $matches[1]), $matches[2]);
@@ -265,8 +266,8 @@ EOF;
             $help
         );
 
-        $help = preg_replace('#^                        #m', '  ', $help);
-        $help = preg_replace('#\*\* +\[#', '** [', $help);
+        $help = Preg::replace('#^                        #m', '  ', $help);
+        $help = Preg::replace('#\*\* +\[#', '** [', $help);
 
         $downloadLatestUrl = sprintf('https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v%s/php-cs-fixer.phar', HelpCommand::getLatestReleaseVersionFromChangeLog());
         $downloadUrl = 'http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar';

+ 2 - 1
src/Console/Command/SelfUpdateCommand.php

@@ -14,6 +14,7 @@ namespace PhpCsFixer\Console\Command;
 
 use PhpCsFixer\Console\SelfUpdate\NewVersionCheckerInterface;
 use PhpCsFixer\PharCheckerInterface;
+use PhpCsFixer\Preg;
 use PhpCsFixer\ToolInfoInterface;
 use Symfony\Component\Console\Command\Command;
 use Symfony\Component\Console\Input\InputInterface;
@@ -99,7 +100,7 @@ EOT
         }
 
         $currentVersion = $this->getApplication()->getVersion();
-        preg_match('/^v?(?<major>\d+)\./', $currentVersion, $matches);
+        Preg::match('/^v?(?<major>\d+)\./', $currentVersion, $matches);
         $currentMajor = (int) $matches['major'];
 
         try {

+ 4 - 3
src/Differ/DiffConsoleFormatter.php

@@ -12,6 +12,7 @@
 
 namespace PhpCsFixer\Differ;
 
+use PhpCsFixer\Preg;
 use Symfony\Component\Console\Formatter\OutputFormatter;
 
 /**
@@ -53,7 +54,7 @@ final class DiffConsoleFormatter
 
         $template = $isDecorated
             ? $this->template
-            : preg_replace('/<[^<>]+>/', '', $this->template)
+            : Preg::replace('/<[^<>]+>/', '', $this->template)
         ;
 
         return sprintf(
@@ -63,7 +64,7 @@ final class DiffConsoleFormatter
                 array_map(
                     function ($string) use ($isDecorated, $lineTemplate) {
                         if ($isDecorated) {
-                            $string = preg_replace_callback(
+                            $string = Preg::replaceCallback(
                                 array(
                                     '/^(\+.*)/',
                                     '/^(\-.*)/',
@@ -86,7 +87,7 @@ final class DiffConsoleFormatter
 
                         return sprintf($lineTemplate, $string);
                     },
-                    preg_split('#\R#u', $diff)
+                    Preg::split('#\R#u', $diff)
                 )
             )
         );

+ 5 - 3
src/DocBlock/Annotation.php

@@ -12,6 +12,8 @@
 
 namespace PhpCsFixer\DocBlock;
 
+use PhpCsFixer\Preg;
+
 /**
  * This represents an entire annotation from a docblock.
  *
@@ -196,7 +198,7 @@ class Annotation
             $content = $this->getTypesContent();
 
             while ('' !== $content && false !== $content) {
-                preg_match(
+                Preg::match(
                     '{^'.self::REGEX_TYPES.'$}x',
                     $content,
                     $matches
@@ -219,7 +221,7 @@ class Annotation
     {
         $pattern = '/'.preg_quote($this->getTypesContent(), '/').'/';
 
-        $this->lines[0]->setContent(preg_replace($pattern, implode('|', $types), $this->lines[0]->getContent(), 1));
+        $this->lines[0]->setContent(Preg::replace($pattern, implode('|', $types), $this->lines[0]->getContent(), 1));
 
         $this->clearCache();
     }
@@ -267,7 +269,7 @@ class Annotation
                 throw new \RuntimeException('This tag does not support types.');
             }
 
-            $matchingResult = preg_match(
+            $matchingResult = Preg::match(
                 '{^(?:\s*\*|/\*\*)\s*@'.$name.'\s+'.self::REGEX_TYPES.'(?:[ \t].*)?$}sx',
                 $this->lines[0]->getContent(),
                 $matches

+ 5 - 3
src/DocBlock/Line.php

@@ -12,6 +12,8 @@
 
 namespace PhpCsFixer\DocBlock;
 
+use PhpCsFixer\Preg;
+
 /**
  * This represents a line of a docblock.
  *
@@ -65,7 +67,7 @@ class Line
      */
     public function containsUsefulContent()
     {
-        return 0 !== preg_match('/\\*\s*\S+/', $this->content) && !$this->isTheStart() && !$this->isTheEnd();
+        return 0 !== Preg::match('/\\*\s*\S+/', $this->content) && !$this->isTheStart() && !$this->isTheEnd();
     }
 
     /**
@@ -77,7 +79,7 @@ class Line
      */
     public function containsATag()
     {
-        return 0 !== preg_match('/\\*\s*@/', $this->content);
+        return 0 !== Preg::match('/\\*\s*@/', $this->content);
     }
 
     /**
@@ -131,7 +133,7 @@ class Line
      */
     public function addBlank()
     {
-        $matched = preg_match('/^([ \t]*\*)[^\r\n]*(\r?\n)$/', $this->content, $matches);
+        $matched = Preg::match('/^([ \t]*\*)[^\r\n]*(\r?\n)$/', $this->content, $matches);
 
         if (1 !== $matched) {
             return;

+ 4 - 2
src/DocBlock/Tag.php

@@ -12,6 +12,8 @@
 
 namespace PhpCsFixer\DocBlock;
 
+use PhpCsFixer\Preg;
+
 /**
  * This represents a tag, as defined by the proposed PSR PHPDoc standard.
  *
@@ -65,7 +67,7 @@ class Tag
     public function getName()
     {
         if (null === $this->name) {
-            preg_match_all('/@[a-zA-Z0-9_-]+(?=\s|$)/', $this->line->getContent(), $matches);
+            Preg::matchAll('/@[a-zA-Z0-9_-]+(?=\s|$)/', $this->line->getContent(), $matches);
 
             if (isset($matches[0][0])) {
                 $this->name = ltrim($matches[0][0], '@');
@@ -92,7 +94,7 @@ class Tag
             throw new \RuntimeException('Cannot set name on unknown tag.');
         }
 
-        $this->line->setContent(preg_replace("/@${current}/", "@${name}", $this->line->getContent(), 1));
+        $this->line->setContent(Preg::replace("/@${current}/", "@${name}", $this->line->getContent(), 1));
 
         $this->name = $name;
     }

+ 3 - 2
src/Doctrine/Annotation/Tokens.php

@@ -13,6 +13,7 @@
 namespace PhpCsFixer\Doctrine\Annotation;
 
 use Doctrine\Common\Annotations\DocLexer;
+use PhpCsFixer\Preg;
 use PhpCsFixer\Tokenizer\Token as PhpToken;
 
 /**
@@ -42,7 +43,7 @@ final class Tokens extends \SplFixedArray
         $ignoredTextPosition = 0;
         $currentPosition = 0;
         while (false !== $nextAtPosition = strpos($content, '@', $currentPosition)) {
-            if (0 !== $nextAtPosition && !preg_match('/\s/', $content[$nextAtPosition - 1])) {
+            if (0 !== $nextAtPosition && !Preg::match('/\s/', $content[$nextAtPosition - 1])) {
                 $currentPosition = $nextAtPosition + 1;
 
                 continue;
@@ -197,7 +198,7 @@ final class Tokens extends \SplFixedArray
                 isset($this[$index + 3])
                 && $this[$index + 2]->isType(DocLexer::T_NONE)
                 && $this[$index + 3]->isType(DocLexer::T_OPEN_PARENTHESIS)
-                && preg_match('/^(\R\s*\*\s*)*\s*$/', $this[$index + 2]->getContent())
+                && Preg::match('/^(\R\s*\*\s*)*\s*$/', $this[$index + 2]->getContent())
             ) {
                 $currentIndex = $index + 3;
             }

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