/** * @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); }
/** * @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()); }
/** * @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()); } }
/** * @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()); } }
/** * @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()); } }
/** * @param FileTool $tool * @param string $input * @param string $expect */ public static function assertFixer(FileTool $tool, $input, $expect) { $path = BaseTestCase::createTempFile($input); $file = new File($path, File::STATUS_UNKNOWN); self::assertNotEmpty($file->getContent()->get()); # content is loaded, we can delete source file unlink($path); $tool->process($file, new Report()); $output = $file->getContent()->get(); static::assertEquals($expect, $output); }
/** * @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()); } }
/** * @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); }
/** * @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()); }
/** * @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()); } }
/** * @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()); }
/** * @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()); }
/** * @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()); }
/** * @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); } }
/** * @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()); }
/** * @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()); }