Example #1
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());
 }
Example #2
0
 /**
  * @param File $file
  * @param Report $report
  */
 public function process(File $file, Report $report)
 {
     $collection = Collection::createFromString($file->getContent()->get());
     (new PatternMatcher($collection))->apply(function (QuerySequence $q) {
         $any = Strict::create()->valueLike('!.+!');
         $q->strict('function');
         $q->strict(T_WHITESPACE);
         $q->strict($any);
         $q->possible(T_WHITESPACE);
         $q->section('(', ')');
         // return type
         $beforeSpace = $q->possible(T_WHITESPACE);
         $identifier = $q->strict(':');
         $afterSpace = $q->possible(T_WHITESPACE);
         $q->strict($any);
         $q->search(Search::create()->valueLike('!^\\{|\\;$!'));
         if ($q->isValid()) {
             $beforeSpace->remove();
             $identifier->prependToValue($this->before);
             // add symbol before
             $identifier->appendToValue($this->after);
             // add symbol after
             $afterSpace->remove();
         }
     });
     $file->getContent()->set((string) $collection);
 }
Example #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());
     }
 }
Example #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());
     }
 }
Example #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());
     }
 }
 /**
  * Updates the value of the constant named $key to $value
  *
  * @param mixed $key
  * @param mixed $value
  * @return string
  */
 public function modify($key, $value)
 {
     $tokens = Collection::createFromString($this->source);
     $query = new Query();
     $query->typeIs(T_CONST);
     $constants = $tokens->find($query);
     foreach ($constants as $constant) {
         $this->handleConstant($tokens, $constant, $key, $value);
     }
     return $tokens->assemble();
 }
Example #7
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());
     }
 }
 /**
  * @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());
     }
 }
Example #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 = $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());
 }
Example #10
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());
 }
Example #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());
     }
 }
 /**
  * @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());
 }
Example #13
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());
 }
Example #14
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);
     }
 }
Example #15
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());
 }
Example #16
0
 /**
  * @param $code
  */
 public function __construct($code)
 {
     $this->collection = Collection::createFromString($code);
 }
Example #17
0
 /**
  * @param string $path
  */
 public function __construct($path)
 {
     $this->path = $path;
     $code = file_get_contents($path);
     $this->collection = Collection::createFromString($code);
 }
Example #18
0
 /**
  * @param File $file
  * @return \Funivan\PhpTokenizer\Collection
  */
 protected function getInvalidStartTokens(File $file)
 {
     $collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get());
     return $collection->find($this->getFindQuery());
 }