/** * @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); } }
/** *@covers \PHP\Manipulator\Token::getTokenName */ public function testGetTokenName() { $token = new Token(' ', T_WHITESPACE); $this->assertEquals('T_WHITESPACE', $token->getTokenName()); $token = new Token('/* Foo */', T_COMMENT); $this->assertEquals('T_COMMENT', $token->getTokenName()); }
/** * 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; }
/** * @param \PHP\Manipulator\Token $token */ public function printToken(Token $token) { $name = str_pad($token->getTokenName(), 28, ' '); $value = $this->transformTokenValue($token->getValue()); return $name . ' | ' . $value; }