Exemplo n.º 1
0
 /**
  * @inheritdoc
  */
 protected function getResultState(InputInterface $input, OutputInterface $output, Report $report)
 {
     $isVerbose = $output->getVerbosity() === OutputInterface::VERBOSITY_DEBUG;
     if ($report->count() === 0) {
         $output->writeln('<info>✔ Looking good</info>');
         return 0;
     }
     $output->writeln('');
     foreach ($report as $message) {
         $output->write('<info>');
         $output->writeln('file    : ' . $message->getFile()->getPath() . ':' . $message->getLine());
         $output->writeln('tool    : ' . $message->getTool()->getName());
         if ($isVerbose) {
             $output->writeln('info    : ' . $message->getTool()->getDescription());
         }
         $output->writeln('message : ' . $message->getText());
         $output->writeln('</info>');
     }
     if ($input->getOption('save')) {
         $output->writeln('<info>✔ Fixed</info>');
     } else {
         $output->writeln('<comment>✔ Dry run</comment>');
     }
     return 0;
 }
Exemplo n.º 2
0
 /**
  * @inheritdoc
  */
 public function process(File $file, Report $report)
 {
     $collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get());
     $stripTokens = $this->findTokens($collection);
     $invalidTokensNum = $stripTokens->count();
     $lastInvalidToken = $this->getLastInvalidToken($collection);
     if (null !== $lastInvalidToken) {
         $invalidTokensNum++;
     }
     if ($invalidTokensNum === 0) {
         return;
     }
     $report->addMessage($file, $this, 'Find empty lines with spaces: ' . $invalidTokensNum);
     foreach ($stripTokens as $token) {
         $value = $token->getValue();
         $token->setValue($this->replaceEmptyLines($value));
     }
     if (null !== $lastInvalidToken) {
         $value = $lastInvalidToken->getValue();
         $value = $this->replaceEmptyLines($value);
         $value = preg_replace('![ ]+(\\n*)$!', '$1', $value);
         $value = preg_replace('!^[ ]+!', '', $value);
         $lastInvalidToken->setValue($value);
     }
     $file->getContent()->set($collection->assemble());
 }
Exemplo n.º 3
0
 /**
  * @param File $file
  * @param Report $report
  * @void
  */
 public function process(File $file, Report $report)
 {
     $collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get());
     $tokens = $this->getInvalidTokens($collection);
     foreach ($tokens as $token) {
         $report->addMessage($file, $this, 'Expect one empty line before class end', $token->getLine());
     }
 }
Exemplo n.º 4
0
 /**
  * @inheritdoc
  */
 public function process(File $file, Report $report)
 {
     $tokens = Collection::createFromString($file->getContent()->get());
     $closedTags = (new TokenFinder($tokens))->find((new Query())->typeIs(T_CLOSE_TAG));
     foreach ($closedTags as $token) {
         $report->addMessage($file, $this, 'File contains closing tag', $token->getLine());
     }
 }
Exemplo n.º 5
0
 /**
  * @inheritdoc
  */
 public function process(File $file, Report $report)
 {
     $collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get());
     $tokens = $collection->find($this->getFindQuery());
     foreach ($tokens as $token) {
         $report->addMessage($file, $this, 'Expect only LF line ending', $token->getLine());
     }
 }
Exemplo n.º 6
0
 /**
  * @param File $file
  * @param Report $report
  */
 public function process(File $file, Report $report)
 {
     $collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get());
     $items = $this->getInvalidStartTokens($collection);
     if (count($items) === 0) {
         return;
     }
     foreach ($items as $lineTokenData) {
         $report->addMessage($file, $this, 'Expect at one empty line after php open tag', $lineTokenData->getToken()->getLine());
     }
 }
Exemplo n.º 7
0
 /**
  * @inheritdoc
  */
 public function process(File $file, Report $report)
 {
     $fileContent = $file->getContent()->get();
     foreach (self::$regexp as $regexp) {
         if (preg_match($regexp, $fileContent) === 1) {
             return;
         }
     }
     $message = 'File must begin with `<?php` or `<?` or `#!/usr/bin/env php`';
     $report->addMessage($file, $this, $message, 1);
 }
Exemplo n.º 8
0
 /**
  * @param File $file
  * @param Report $report
  * @void
  */
 public function process(File $file, Report $report)
 {
     $collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get());
     $tokens = $collection->find($this->getFindQuery());
     foreach ($tokens as $token) {
         $value = $token->getValue();
         $value = preg_replace(self::REGEX, "\n", $value);
         $token->setValue($value);
         $report->addMessage($file, $this, 'Replace invalid line ending', $token->getLine());
     }
     $file->getContent()->set($collection->assemble());
 }
Exemplo n.º 9
0
 /**
  * @param File $file
  * @param Report $report
  * @void
  */
 public function process(File $file, Report $report)
 {
     $collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get());
     $tokens = $this->getInvalidTokens($collection);
     $emptyLines = "\n" . str_repeat("\n", $this->getLinesNum());
     foreach ($tokens as $token) {
         $report->addMessage($file, $this, 'Set one line before closing tag', $token->getLine());
         $newValue = $this->getTokenNewValue($token, $emptyLines);
         $token->setValue($newValue);
     }
     $file->getContent()->set($collection->assemble());
 }
Exemplo n.º 10
0
 /**
  * @param File $file
  * @param Report $report
  */
 public function process(File $file, Report $report)
 {
     $collection = Collection::createFromString($file->getContent()->get());
     $invalidProperties = InvalidPropertyFinder::find($collection);
     if (count($invalidProperties) === 0) {
         return;
     }
     foreach ($invalidProperties as $property) {
         $variable = $property->getVariable();
         $report->addMessage($file, $this, 'Invalid property. Redundant NULL value for the property: ' . $variable->getValue(), $variable->getLine());
     }
 }
Exemplo n.º 11
0
 /**
  * @inheritdoc
  */
 public function process(File $file, Report $report)
 {
     $collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get());
     $tags = $this->findTags($collection);
     if ($tags->count() === 0) {
         return;
     }
     $type = $this->useFullTags() ? 'long' : 'short';
     $message = 'You should use only ' . $type . ' php tags';
     foreach ($tags as $tag) {
         $report->addMessage($file, $this, $message, $tag->getLine());
     }
 }
Exemplo n.º 12
0
 /**
  * @param File $file
  * @param Report $report
  */
 public function process(File $file, Report $report)
 {
     $collection = Collection::createFromString($file->getContent()->get());
     $invalidProperties = InvalidPropertyFinder::find($collection);
     if (count($invalidProperties) === 0) {
         return;
     }
     foreach ($invalidProperties as $property) {
         $report->addMessage($file, $this, 'Replace redundant NULL value', $property->getVariable()->getLine());
         foreach ($property->getTokensToReplace() as $token) {
             $token->remove();
         }
     }
     $file->getContent()->set($collection->assemble());
 }
Exemplo n.º 13
0
 /**
  * @inheritdoc
  */
 public function process(File $file, Report $report)
 {
     $process = new Process(sprintf('composer validate %s', $file->getPath()));
     $process->run();
     if ($process->isSuccessful()) {
         return;
     }
     $errorOutput = $process->getErrorOutput();
     preg_match('!Parse error on line (\\d+):!', $errorOutput, $matchedLine);
     $line = 1;
     if (isset($matchedLine[1])) {
         $line = (int) $matchedLine[1];
     }
     $report->addMessage($file, $this, 'Invalid composer.json file format', $line);
 }
Exemplo n.º 14
0
 /**
  * @inheritdoc
  */
 public function process(File $file, Report $report)
 {
     $collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get());
     $tags = $this->findTags($collection);
     if ($tags->count() === 0) {
         return;
     }
     $type = $this->useFullTags() ? 'long' : 'short';
     $message = 'Detect ' . $type . ' php tag';
     $newTag = $this->useShortTags() ? '<?' : '<?php';
     foreach ($tags as $tag) {
         $report->addMessage($file, $this, $message, $tag->getLine());
         $spaces = preg_replace('!^(\\S+)(\\s)!', '$2', $tag->getValue());
         if ($spaces === $tag->getValue()) {
             $spaces = '';
         }
         $tag->setValue($newTag . $spaces);
     }
     $file->getContent()->set($collection->assemble());
 }
Exemplo n.º 15
0
 /**
  * @param FilesCollection $files
  * @param Report $report
  */
 public function process(FilesCollection $files, Report $report)
 {
     $isDebug = $this->getOutput()->isDebug();
     foreach ($files as $file) {
         $errorsNum = $report->count();
         if ($isDebug) {
             $this->getOutput()->writeln('⚑ open  : ' . $file->getPath());
         }
         foreach ($this->getTools() as $tool) {
             if (!$tool->canProcess($file)) {
                 continue;
             }
             $tool->process($file, $report);
             if ($errorsNum === $report->count()) {
                 if ($isDebug) {
                     $this->getOutput()->writeln('✔ ok    : ' . $tool->getDescription() . ' (' . $tool->getName() . ')');
                 }
                 continue;
             }
             $this->getOutput()->writeln('✘ error : ' . $tool->getDescription() . ' (' . $tool->getName() . ')');
         }
     }
 }
Exemplo n.º 16
0
 /**
  * @inheritdoc
  */
 public function process(File $file, Report $report)
 {
     $collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get());
     $tokens = $this->findTokens($collection);
     $lastInvalidToken = $this->getLastInvalidToken($collection);
     if (null !== $lastInvalidToken) {
         $tokens->append($lastInvalidToken);
     }
     if ($tokens->count() === 0) {
         return;
     }
     $lines = [];
     foreach ($tokens as $token) {
         $line = $token->getLine();
         $line++;
         # Our token start in previous line
         if (isset($lines[$line])) {
             continue;
         }
         $lines[$line] = true;
         $report->addMessage($file, $this, 'File contains empty lines with spaces.', $line);
     }
 }
Exemplo n.º 17
0
 /**
  * @param File $file
  * @param Report $report
  * @void
  */
 public function process(File $file, Report $report)
 {
     $collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get());
     $items = $this->getInvalidStartTokens($collection);
     if (count($items) === 0) {
         return;
     }
     foreach ($items as $tokenInfo) {
         /** @var Token $token */
         $whitespace = $tokenInfo->getWhitespace();
         $token = $tokenInfo->getToken();
         $tokenValue = $token->getValue();
         $whitespaceValue = $whitespace->getValue() ? $whitespace->getValue() : '';
         $whitespace->remove();
         preg_match('!([ ]+)$!', $whitespaceValue, $endSpaces);
         $whitespaceValue = !empty($endSpaces[1]) ? $endSpaces[1] : '';
         $lines = explode("\n", $tokenValue);
         $tokenValue = reset($lines);
         $append = $tokenValue . "\n\n" . $whitespaceValue;
         $token->setValue($append);
         $report->addMessage($file, $this, 'Set one empty line after php open tag', $token->getLine());
     }
     $file->getContent()->set($collection->assemble());
 }
Exemplo n.º 18
0
 /**
  * @inheritdoc
  */
 public function process(File $file, Report $report)
 {
     $cmd = sprintf('php --syntax-check %s', $file->getPath());
     $process = new Process($cmd);
     $process->run();
     # Create the array of outputs and remove empty values.
     $output = array_filter(explode(PHP_EOL, $process->getOutput()));
     if ($process->isSuccessful()) {
         return;
     }
     $regex = '!\\s+on line (\\d+)!';
     $needle = 'Parse error: syntax error, ';
     foreach (array_slice($output, 0, count($output) - 1) as $error) {
         $raw = ucfirst(substr($error, strlen($needle)));
         $message = str_replace(' in ' . $file->getPath(), '', $raw);
         $line = 0;
         preg_match($regex, $message, $lineMatch);
         if (isset($lineMatch[1])) {
             $line = (int) $lineMatch[1];
             $message = preg_replace($regex, '', $message);
         }
         $report->addMessage($file, $this, $message, $line);
     }
 }