Skip to content

Bug #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 17, 2020
Merged

Bug #110

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions SymfonyCustom/Helpers/AbstractHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace SymfonyCustom\Helpers;

/**
* Class AbstractHelper
*/
abstract class AbstractHelper
{
private function __construct()
{
}
}
6 changes: 2 additions & 4 deletions SymfonyCustom/Helpers/FixerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
use PHP_CodeSniffer\Files\File;

/**
* class FixerHelper
*
* @internal
* Class FixerHelper
*/
class FixerHelper
class FixerHelper extends AbstractHelper
{
/**
* @param File $phpcsFile
Expand Down
6 changes: 2 additions & 4 deletions SymfonyCustom/Helpers/SniffHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
use PHP_CodeSniffer\Util\Tokens;

/**
* class SniffHelper
*
* @internal
* Class SniffHelper
*/
class SniffHelper
class SniffHelper extends AbstractHelper
{
public const TAGS = [
'@api',
Expand Down
5 changes: 5 additions & 0 deletions TwigCS/src/Ruleset/Generic/OperatorSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ private function isUnary(int $tokenPosition, array $tokens): bool
return true;
}

if ($this->isTokenMatching($previousToken, Token::BLOCK_TAG_TYPE)) {
// {% if -2 ... %}
return true;
}

return false;
}
}
133 changes: 36 additions & 97 deletions TwigCS/src/Runner/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,90 +172,12 @@ public function fixFile(string $file): bool
return true;
}

/**
* @param string $filePath File path to diff the file against.
*
* @return string
*/
public function generateDiff(string $filePath): string
{
$cwd = getcwd().DIRECTORY_SEPARATOR;
if (strpos($filePath, $cwd) === 0) {
$filename = substr($filePath, strlen($cwd));
} else {
$filename = $filePath;
}

$contents = $this->getContents();

$tempName = tempnam(sys_get_temp_dir(), 'phpcs-fixer');
$fixedFile = fopen($tempName, 'w');
fwrite($fixedFile, $contents);

// We must use something like shell_exec() because whitespace at the end
// of lines is critical to diff files.
$filename = escapeshellarg($filename);
$cmd = "diff -u -L$filename -LPHP_CodeSniffer $filename \"$tempName\"";

$diff = shell_exec($cmd);

fclose($fixedFile);
if (is_file($tempName) === true) {
unlink($tempName);
}

$diffLines = null !== $diff ? explode(PHP_EOL, $diff) : [];
if (count($diffLines) === 1) {
// Seems to be required for cygwin.
$diffLines = explode("\n", $diff);
}

$diff = [];
foreach ($diffLines as $line) {
if (isset($line[0]) === true) {
switch ($line[0]) {
case '-':
$diff[] = "\033[31m$line\033[0m";
break;
case '+':
$diff[] = "\033[32m$line\033[0m";
break;
default:
$diff[] = $line;
}
}
}

$diff = implode(PHP_EOL, $diff);

return $diff;
}

/**
* @return string
*/
public function getContents(): string
{
$contents = implode($this->tokens);

return $contents;
}

/**
* This function takes changesets into account so should be used
* instead of directly accessing the token array.
*
* @param int $tokenPosition
*
* @return string
*/
public function getTokenContent(int $tokenPosition): string
{
if ($this->inChangeset && isset($this->changeset[$tokenPosition])) {
return $this->changeset[$tokenPosition];
}

return $this->tokens[$tokenPosition];
return implode($this->tokens);
}

/**
Expand Down Expand Up @@ -367,24 +289,6 @@ public function replaceToken(int $tokenPosition, string $content): bool
return true;
}

/**
* @param int $tokenPosition
*
* @return bool
*/
public function revertToken(int $tokenPosition): bool
{
if (!isset($this->fixedTokens[$tokenPosition])) {
return false;
}

$this->tokens[$tokenPosition] = $this->fixedTokens[$tokenPosition];
unset($this->fixedTokens[$tokenPosition]);
$this->numFixes--;

return true;
}

/**
* @param int $tokenPosition
*
Expand Down Expand Up @@ -434,4 +338,39 @@ public function addContentBefore(int $tokenPosition, string $content): bool

return $this->replaceToken($tokenPosition, $content.$current);
}

/**
* This function takes changesets into account so should be used
* instead of directly accessing the token array.
*
* @param int $tokenPosition
*
* @return string
*/
protected function getTokenContent(int $tokenPosition): string
{
if ($this->inChangeset && isset($this->changeset[$tokenPosition])) {
return $this->changeset[$tokenPosition];
}

return $this->tokens[$tokenPosition];
}

/**
* @param int $tokenPosition
*
* @return bool
*/
protected function revertToken(int $tokenPosition): bool
{
if (!isset($this->fixedTokens[$tokenPosition])) {
return false;
}

$this->tokens[$tokenPosition] = $this->fixedTokens[$tokenPosition];
unset($this->fixedTokens[$tokenPosition]);
$this->numFixes--;

return true;
}
}
4 changes: 2 additions & 2 deletions TwigCS/src/Runner/Linter.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function run(array $files, Ruleset $ruleset, bool $fix = false): Report
*
* @throws Exception
*/
public function fix(iterable $files, Ruleset $ruleset): void
protected function fix(iterable $files, Ruleset $ruleset): void
{
$fixer = new Fixer($ruleset, $this->tokenizer);

Expand All @@ -113,7 +113,7 @@ public function fix(iterable $files, Ruleset $ruleset): void
*
* @return bool
*/
public function processTemplate(string $file, Ruleset $ruleset, Report $report): bool
protected function processTemplate(string $file, Ruleset $ruleset, Report $report): bool
{
$twigSource = new Source(file_get_contents($file), $file);

Expand Down
60 changes: 23 additions & 37 deletions TwigCS/src/Sniff/AbstractSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,30 @@ public function disable(): void
$this->fixer = null;
}

/**
* @param array $stream
*/
public function processFile(array $stream): void
{
foreach ($stream as $index => $token) {
$this->process($index, $stream);
}
}

/**
* @param int $tokenPosition
* @param Token[] $stream
*/
abstract protected function process(int $tokenPosition, array $stream): void;

/**
* @param Token $token
* @param int|array $type
* @param string|array $value
*
* @return bool
*/
public function isTokenMatching(Token $token, $type, $value = []): bool
protected function isTokenMatching(Token $token, $type, $value = []): bool
{
if (!is_array($type)) {
$type = [$type];
Expand All @@ -74,7 +90,7 @@ public function isTokenMatching(Token $token, $type, $value = []): bool
*
* @return int|false
*/
public function findNext($type, array $tokens, int $start, bool $exclude = false)
protected function findNext($type, array $tokens, int $start, bool $exclude = false)
{
$i = 0;

Expand All @@ -97,7 +113,7 @@ public function findNext($type, array $tokens, int $start, bool $exclude = false
*
* @return int|false
*/
public function findPrevious($type, array $tokens, int $start, bool $exclude = false)
protected function findPrevious($type, array $tokens, int $start, bool $exclude = false)
{
$i = 0;

Expand All @@ -118,7 +134,7 @@ public function findPrevious($type, array $tokens, int $start, bool $exclude = f
*
* @throws Exception
*/
public function addWarning(string $message, Token $token): void
protected function addWarning(string $message, Token $token): void
{
$this->addMessage(Report::MESSAGE_TYPE_WARNING, $message, $token);
}
Expand All @@ -129,7 +145,7 @@ public function addWarning(string $message, Token $token): void
*
* @throws Exception
*/
public function addError(string $message, Token $token): void
protected function addError(string $message, Token $token): void
{
$this->addMessage(Report::MESSAGE_TYPE_ERROR, $message, $token);
}
Expand All @@ -142,7 +158,7 @@ public function addError(string $message, Token $token): void
*
* @throws Exception
*/
public function addFixableWarning(string $message, Token $token): bool
protected function addFixableWarning(string $message, Token $token): bool
{
return $this->addFixableMessage(Report::MESSAGE_TYPE_WARNING, $message, $token);
}
Expand All @@ -155,41 +171,11 @@ public function addFixableWarning(string $message, Token $token): bool
*
* @throws Exception
*/
public function addFixableError(string $message, Token $token): bool
protected function addFixableError(string $message, Token $token): bool
{
return $this->addFixableMessage(Report::MESSAGE_TYPE_ERROR, $message, $token);
}

/**
* @param Token $token
*
* @return string|null
*/
public function stringifyValue(Token $token): ?string
{
if ($token->getType() === Token::STRING_TYPE) {
return $token->getValue();
}

return '\''.$token->getValue().'\'';
}

/**
* @param array $stream
*/
public function processFile(array $stream): void
{
foreach ($stream as $index => $token) {
$this->process($index, $stream);
}
}

/**
* @param int $tokenPosition
* @param Token[] $stream
*/
abstract protected function process(int $tokenPosition, array $stream): void;

/**
* @param int $messageType
* @param string $message
Expand Down
4 changes: 2 additions & 2 deletions TwigCS/src/Sniff/AbstractSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ abstract protected function shouldHaveSpaceBefore(int $tokenPosition, array $tok
*
* @throws Exception
*/
protected function checkSpaceAfter(int $tokenPosition, array $tokens, int $expected): void
private function checkSpaceAfter(int $tokenPosition, array $tokens, int $expected): void
{
$token = $tokens[$tokenPosition];

Expand Down Expand Up @@ -94,7 +94,7 @@ protected function checkSpaceAfter(int $tokenPosition, array $tokens, int $expec
*
* @throws Exception
*/
protected function checkSpaceBefore(int $tokenPosition, array $tokens, int $expected): void
private function checkSpaceBefore(int $tokenPosition, array $tokens, int $expected): void
{
$token = $tokens[$tokenPosition];

Expand Down
21 changes: 12 additions & 9 deletions TwigCS/src/Token/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ class Token
public const INTERPOLATION_END_TYPE = 11;
public const ARROW_TYPE = 12;
// New constants
public const WHITESPACE_TYPE = 13;
public const TAB_TYPE = 14;
public const EOL_TYPE = 15;
public const COMMENT_START_TYPE = 16;
public const COMMENT_TEXT_TYPE = 17;
public const COMMENT_WHITESPACE_TYPE = 18;
public const COMMENT_TAB_TYPE = 19;
public const COMMENT_EOL_TYPE = 20;
public const COMMENT_END_TYPE = 21;
public const DQ_STRING_START_TYPE = 13;
public const DQ_STRING_END_TYPE = 14;
public const BLOCK_TAG_TYPE = 15;
public const WHITESPACE_TYPE = 16;
public const TAB_TYPE = 17;
public const EOL_TYPE = 18;
public const COMMENT_START_TYPE = 19;
public const COMMENT_TEXT_TYPE = 20;
public const COMMENT_WHITESPACE_TYPE = 21;
public const COMMENT_TAB_TYPE = 22;
public const COMMENT_EOL_TYPE = 23;
public const COMMENT_END_TYPE = 24;

public const EMPTY_TOKENS = [
self::WHITESPACE_TYPE => self::WHITESPACE_TYPE,
Expand Down
Loading