示例#1
0
 private function getBestDelimiter($pattern)
 {
     $delimiters = array();
     foreach (self::$delimiters as $k => $d) {
         if (false === strpos($pattern, $d)) {
             return $d;
         }
         $delimiters[$d] = array(substr_count($pattern, $d), $k);
     }
     uasort($delimiters, function ($a, $b) {
         if ($a[0] === $b[0]) {
             return Utils::cmpInt($a, $b);
         }
         return $a[0] < $b[0] ? -1 : 1;
     });
     return key($delimiters);
 }
示例#2
0
 protected function getFixersHelp()
 {
     $help = '';
     $maxName = 0;
     $fixers = $this->fixer->getFixers();
     // sort fixers by level and name
     usort($fixers, function (FixerInterface $a, FixerInterface $b) {
         $cmp = Utils::cmpInt($a->getLevel(), $b->getLevel());
         if (0 !== $cmp) {
             return $cmp;
         }
         return strcmp($a->getName(), $b->getName());
     });
     foreach ($fixers as $fixer) {
         if (strlen($fixer->getName()) > $maxName) {
             $maxName = strlen($fixer->getName());
         }
     }
     $count = count($fixers) - 1;
     foreach ($fixers as $i => $fixer) {
         $chunks = explode("\n", wordwrap(sprintf("[%s]\n%s", $this->fixer->getLevelAsString($fixer), $fixer->getDescription()), 72 - $maxName, "\n"));
         $help .= sprintf(" * <comment>%s</comment>%s %s\n", $fixer->getName(), str_repeat(' ', $maxName - strlen($fixer->getName())), array_shift($chunks));
         while ($c = array_shift($chunks)) {
             $help .= str_repeat(' ', $maxName + 4) . $c . "\n";
         }
         if ($count !== $i) {
             $help .= "\n";
         }
     }
     return $help;
 }
示例#3
0
 private function sortFixers()
 {
     usort($this->fixers, function (FixerInterface $a, FixerInterface $b) {
         return Utils::cmpInt($b->getPriority(), $a->getPriority());
     });
 }
示例#4
0
 /**
  * Sort fixers by their priorities.
  *
  * @return $this
  */
 private function sortFixers()
 {
     // Schwartzian transform is used to improve the efficiency and avoid
     // `usort(): Array was modified by the user comparison function` warning for mocked objects.
     $data = array_map(function (FixerInterface $fixer) {
         return array($fixer, $fixer->getPriority());
     }, $this->fixers);
     usort($data, function (array $a, array $b) {
         return Utils::cmpInt($b[1], $a[1]);
     });
     $this->fixers = array_map(function (array $item) {
         return $item[0];
     }, $data);
     return $this;
 }
示例#5
0
 /**
  * Sort registered Transformers.
  */
 private function sortTransformers()
 {
     usort($this->items, function (TransformerInterface $a, TransformerInterface $b) {
         return Utils::cmpInt($b->getPriority(), $a->getPriority());
     });
 }
示例#6
0
 /**
  * @param FixerInterface[] $fixers
  *
  * @return FixerInterface[]
  */
 private function sortFixers(array $fixers)
 {
     usort($fixers, function (FixerInterface $a, FixerInterface $b) {
         return Utils::cmpInt($b->getPriority(), $a->getPriority());
     });
     return $fixers;
 }
 /**
  * Get the delimiter that would require the least escaping in a regular expression.
  *
  * @param string $pattern the regular expression
  *
  * @return string the preg delimiter
  */
 private function getBestDelimiter($pattern)
 {
     // try do find something that's not used
     $delimiters = array();
     foreach (self::$delimiters as $k => $d) {
         if (false === strpos($pattern, $d)) {
             return $d;
         }
         $delimiters[$d] = array(substr_count($pattern, $d), $k);
     }
     // return the least used delimiter, using the position in the list as a tie breaker
     uasort($delimiters, function ($a, $b) {
         if ($a[0] === $b[0]) {
             return Utils::cmpInt($a, $b);
         }
         return $a[0] < $b[0] ? -1 : 1;
     });
     return key($delimiters);
 }
示例#8
0
 /**
  * @dataProvider provideCmpIntCases
  */
 public function testCmpInt($expected, $left, $right)
 {
     $this->assertSame($expected, Utils::cmpInt($left, $right));
 }