/** * @param \PHP\Manipulator\Token $other * @param string $description Additional information about the test * @param bool $returnResult Whether to return a result or throw an exception * @return boolean */ public function evaluate($other, $description = '', $returnResult = FALSE) { if (!$other instanceof Token) { throw \PHPUnit_Util_InvalidArgumentHelper::factory(1, 'PHP\\Manipulator\\Token'); } $expectedToken = $this->_expectedToken; $equal = $this->_getEqualsConstraint($expectedToken->getValue()); if (!$equal->evaluate($other->getValue(), $description, true)) { $this->_difference = 'values'; if ($returnResult) { return FALSE; } $this->fail($other, $description); } $equal = $this->_getEqualsConstraint($expectedToken->getType()); if (!$equal->evaluate($other->getType(), $description, true)) { $this->_difference = 'types'; if ($returnResult) { return FALSE; } $this->fail($other, $description); } if (true === $this->_strict) { $equal = $this->_getEqualsConstraint($expectedToken->getLinenumber()); if (!$equal->evaluate($other->getLinenumber(), $description, true)) { $this->_difference = 'linenumber'; if ($returnResult) { return FALSE; } $this->fail($other, $description); } } return true; }
/** * Evaluate if the token is * * @param \PHP\Manipulator\Token $token * @param mixed $param * @return boolean */ public function evaluate(Token $token, $params = null) { if (null === $token->getType() && '@' === $token->getValue()) { return true; } return false; }
/** * @covers \PHP\Manipulator\Token::setValue * @covers \PHP\Manipulator\Token::getValue */ public function testSetValueAndGetValue() { $token = new Token('foo'); $this->assertEquals('foo', $token->getValue(), 'wrong value'); $fluent = $token->setValue('bla'); $this->assertSame($fluent, $token, 'No fluent interface'); $this->assertEquals('bla', $token->getValue(), 'wrong value'); }
/** * Evaluate if the token is a multiline comment * * @param \PHP\Manipulator\Token $token * @param mixed $param * @return boolean */ public function evaluate(Token $token, $params = null) { $pattern = '~^(\\n|\\r\\n|\\r)~'; if (preg_match($pattern, $token->getValue())) { return true; } return false; }
/** * Evaluate if the token is a multiline comment * * @param \PHP\Manipulator\Token $token * @param mixed $param * @return boolean */ public function evaluate(Token $token, $param = null) { $value = $token->getValue(); if ($value === "\n" || $value === "\r\n" || $value === "\r") { return true; } return false; }
/** * @param \PHP\Manipulator\Token $token * @param mixed $default * @return boolean|string */ protected function _getNewlineFromToken(Token $token, $default = false) { $matches = array(); if (preg_match("~(\r\n|\r|\n)~", $token->getValue(), $matches)) { return $matches[0]; } return $default; }
/** * @param \PHP\Manipulator\Token $token * @return boolean */ protected function _isOperatorWithoutToken(Token $token) { foreach ($this->_operatorsWithoutTokens as $operator) { if (null === $token->getType() && $operator === $token->getValue()) { return true; } } return false; }
/** * Manipulates a Token * * @param \PHP\Manipulator\Token $token * @param mixed $params */ public function manipulate(Token $token, $params = null) { $value = $token->getValue(); if (substr($value, 0, 2) === "\r\n") { $token->setValue(substr($value, 2)); } elseif (substr($value, 0, 1) === "\n") { $token->setValue(substr($value, 1)); } elseif (substr($value, 0, 1) === "\r") { $token->setValue(substr($value, 1)); } }
/** * Evaluate if the token is a Singleline comment * * @param \PHP\Manipulator\Token $token * @param mixed $param * @return boolean */ public function evaluate(Token $token, $param = null) { if ($token->getType() === T_COMMENT) { $value = $token->getValue(); if (strlen($value) >= 1 && substr($value, 0, 1) === '#') { return true; } elseif (strlen($value) >= 2 && substr($value, 0, 2) === '//') { return true; } } return false; }
/** * Manipulate Token * * @param \PHP\Manipulator\Token $token * @param mixed $params */ public function manipulate(Token $token, $params = null) { $regexNewline = '(\\n|\\r\\n|\\r)'; $indention = $params; $value = $token->getValue(); $lines = preg_split('~' . $regexNewline . '~', $value); $helper = new NewlineDetector(); $newline = $helper->getNewlineFromToken($token); $first = true; $value = ''; foreach ($lines as $key => $line) { if ($first) { $first = false; } else { $temp = trim($line); if (!empty($temp)) { $lines[$key] = $indention . ' ' . $line; } } } $token->setValue(implode($newline, $lines)); }
/** * Token $token * @return boolean */ protected function _containsEscapeSequence(Token $token) { return (bool) preg_match('~' . preg_quote('\\', '~') . '~', $token->getValue()); }
/** * @param \PHP\Manipulator\Token $token * @param array $whitespaces * @return mixed */ public function getWhitespaceForToken(Token $token, array $whitespaces) { if (null === $token->getType()) { $tokenval = $token->getValue(); } else { $tokenval = $token->getType(); } if (array_key_exists($tokenval, $whitespaces)) { return $whitespaces[$tokenval]; } else { $message = 'No option found for: ' . $token->getTokenName() . ' (' . $tokenval . ')'; throw new \Exception($message); } }
/** * @param \PHP\Manipulator\Token $token * @return boolean */ public function isQuestionMark(Token $token) { if ($token->getType() === null && $token->getValue() === '?') { return true; } return false; }
/** * Lowercase for tokens value * * @param \PHP\Manipulator\Token $token * @param mixed $params */ public function manipulate(Token $token, $params = null) { $token->setValue(strtolower($token->getValue())); }
/** * @param \PHP\Manipulator\Token $whitespaceToken */ protected function _indentWhitespace(Token $whitespaceToken) { $newValue = $whitespaceToken->getValue() . $this->getIndention($this->getIndentionLevel()); $whitespaceToken->setValue($newValue); }
/** * @param \PHP\Manipulator\Token $token */ public function printToken(Token $token) { $name = str_pad($token->getTokenName(), 28, ' '); $value = $this->transformTokenValue($token->getValue()); return $name . ' | ' . $value; }
/** * Evaluate if the token * * @param \PHP\Manipulator\Token $token * @param mixed $param * @return boolean */ public function evaluate(Token $token, $params = null) { return (bool) preg_match("~(\r|\n)~", $token->getValue()); }
/** * Dump a token * * Replaces spaces, linebreaks and tabs with visual representations: * \t \r\n \n \r . * * @param \PHP\Manipulator\Token $token * @return string */ public static function dumpToken(Token $token, $add = true) { $type = $token->getType(); $value = $token->getValue(); $typeName = '[SIMPLE]'; if (null !== $type) { $typeName = $token->getTokenName(); } $length = (string) mb_strlen($value, 'utf-8'); $search = array("\n\r", "\n", "\r", "\t", ' '); $replace = array('\\n\\\\r', '\\n', '\\r', '\\t', '.'); $value = str_replace($search, $replace, $value); $line = $token->getLinenumber(); if (null === $line) { $line = 'NULL'; } return str_pad($typeName, 28, ' ', STR_PAD_RIGHT) . '| ' . str_pad($length, 4, ' ', STR_PAD_LEFT) . ' | ' . str_pad($line, 4, ' ', STR_PAD_LEFT) . ' | ' . $value; }
/** * Evaluate if the token only contains whitespace * * @param \PHP\Manipulator\Token $token * @param mixed $param * @return boolean */ public function evaluate(Token $token, $params = null) { return (bool) preg_match('~^(\\s)*$~', $token->getValue()); }