Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Mar 10, 2017
1 parent 1fc449a commit ad681f4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
Expand Up @@ -22,6 +22,11 @@ final class OneObjectOperatorPerLineSniff implements Sniff
*/
private $callerTokens;

/**
* mixed[]
*/
private $tokens;

/**
* @return int[]
*/
Expand All @@ -38,26 +43,24 @@ public function process(File $file, $position): void
{
$this->file = $file;
$this->position = $position;
$this->tokens = $file->getTokens();

$this->callerTokens = [];

$tokens = $file->getTokens();
$pointer = $this->ignoreWhitespace($tokens, $position + 1);
// $tokens = $file->getTokens();
$pointer = $this->ignoreWhitespace($position + 1);

$token = $tokens[$position];
$token = $this->tokens[$position];
$isOwnCall = ($token['content'] === '$this');

try {
$this->handleObjectOperators($tokens, $pointer, $isOwnCall);
} catch (\Exception $exception) {
return;
}
$this->handleObjectOperators($pointer, $isOwnCall);
}

private function ignoreWhitespace(array $tokens, int $start): int
private function ignoreWhitespace(int $start): int
{
$pointer = $start;

while ($tokens[$pointer]['code'] === T_WHITESPACE) {
while ($this->tokens[$pointer]['code'] === T_WHITESPACE) {
++$pointer;
}

Expand All @@ -68,8 +71,6 @@ private function handleTwoObjectOperators(bool $isOwnCall): void
{
if ($this->callerTokens && !$isOwnCall) {
$this->file->addError('Only one object operator per line.', $this->position, self::class);

throw new \Exception();
}
}

Expand All @@ -89,17 +90,15 @@ private function handleExcludedFluentInterfaces(array $tmpToken, string $tmpToke
($memberTokenType === 'method' && $tmpTokenType === 'method' && $memberTokenCount > 1 && $memberToken['token']['content'] !== $tmpToken['content'])
) {
$this->file->addError('Only one object operator per line.', $this->position, self::class);

throw new \Exception();
}
}

private function handleObjectOperators(array $tokens, int $pointer, bool $isOwnCall): void
private function handleObjectOperators(int $pointer, bool $isOwnCall): void
{
while ($tokens[$pointer]['code'] === T_OBJECT_OPERATOR) {
$tmpToken = $tokens[++$pointer];
$pointer = $this->ignoreWhitespace($tokens, $pointer + 1);
$tmpTokenType = $this->getTokenType($tokens[$pointer]);
while ($this->tokens[$pointer]['code'] === T_OBJECT_OPERATOR) {
$tmpToken = $this->tokens[++$pointer];
$pointer = $this->ignoreWhitespace($pointer + 1);
$tmpTokenType = $this->getTokenType($this->tokens[$pointer]);

// Look for second object operator token on same statement
$this->handleTwoObjectOperators($isOwnCall);
Expand All @@ -110,7 +109,7 @@ private function handleObjectOperators(array $tokens, int $pointer, bool $isOwnC
'type' => $tmpTokenType,
];

$pointer = $this->movePointerToNextObject($tokens, $pointer);
$pointer = $this->movePointerToNextObject($pointer);
}
}

Expand All @@ -123,15 +122,15 @@ private function getTokenType(array $token): string
return 'property';
}

private function movePointerToNextObject(array $tokens, int $pointer): int
private function movePointerToNextObject(int $pointer): int
{
$token = $tokens[$pointer];
$token = $this->tokens[$pointer];

// Ignore "(" ... ")" in a method call by moving pointer after close parenthesis token
if ($token['code'] === T_OPEN_PARENTHESIS) {
$pointer = $token['parenthesis_closer'] + 1;
}

return $this->ignoreWhitespace($tokens, $pointer);
return $this->ignoreWhitespace($pointer);
}
}
Expand Up @@ -59,5 +59,3 @@ class PHPSessionStorage
$user = new User();

$user->sessionStorage->start();

?>

0 comments on commit ad681f4

Please sign in to comment.