/** * @param \Twig_TokenStream $tokens * @param integer $position * @param message $target */ private function assertSpacing(\Twig_TokenStream $tokens, $position, $target) { $token = $tokens->look($position); if ($token->getType() !== Token::WHITESPACE_TYPE || strlen($token->getValue()) < $this->spacing) { $this->addViolation($tokens->getFilename(), $token->getLine(), $token->getColumn(), sprintf('There should be %d space(s) %s.', $this->spacing, $target)); } if ($token->getType() === Token::WHITESPACE_TYPE && strlen($token->getValue()) > $this->spacing) { $this->addViolation($tokens->getFilename(), $token->getLine(), $token->getColumn(), sprintf('More than %d space(s) found %s.', $this->spacing, $target)); } }
/** * {@inheritdoc} */ public function check(\Twig_TokenStream $tokens) { $this->reset(); $macros = []; while (!$tokens->isEOF()) { $token = $tokens->getCurrent(); if ($token->getType() === \Twig_Token::NAME_TYPE && $token->getValue() === 'import') { while ($tokens->getCurrent()->getValue() !== 'as') { $tokens->next(); } $tokens->next(); while (in_array($tokens->getCurrent()->getType(), [\Twig_Token::NAME_TYPE, \Twig_Token::PUNCTUATION_TYPE, Token::WHITESPACE_TYPE])) { $next = $tokens->getCurrent(); if ($next->getType() === \Twig_Token::NAME_TYPE) { $macros[$next->getValue()] = $next; } $tokens->next(); } } elseif ($token->getType() === \Twig_Token::NAME_TYPE && array_key_exists($token->getValue(), $macros)) { unset($macros[$token->getValue()]); } $tokens->next(); } foreach ($macros as $name => $originalToken) { $this->addViolation($tokens->getFilename(), $originalToken->getLine(), $originalToken->getColumn(), sprintf('Unused macro "%s".', $name)); } return $this->violations; }
/** * @group legacy */ public function testLegacyConstructorSignature() { $stream = new Twig_TokenStream(array(), 'foo', '{{ foo }}'); $this->assertEquals('foo', $stream->getFilename()); $this->assertEquals('{{ foo }}', $stream->getSource()); $this->assertEquals('foo', $stream->getSourceContext()->getName()); $this->assertEquals('{{ foo }}', $stream->getSourceContext()->getCode()); }
/** * {@inheritdoc} */ public function check(\Twig_TokenStream $tokens) { $this->reset(); while (!$tokens->isEOF()) { $token = $tokens->getCurrent(); if ($token->getType() === \Twig_Token::NAME_TYPE && preg_match('/[A-Z]/', $token->getValue())) { if ($tokens->look(Lexer::PREVIOUS_TOKEN)->getType() === Token::WHITESPACE_TYPE && $tokens->look(-2)->getValue() === 'set') { $this->addViolation($tokens->getFilename(), $token->getLine(), $token->getColumn(), sprintf('The "%s" variable should be in lower case (use _ as a separator).', $token->getValue())); } } $tokens->next(); } return $this->violations; }
/** * {@inheritdoc} */ public function check(\Twig_TokenStream $tokens) { $this->reset(); while (!$tokens->isEOF()) { $token = $tokens->getCurrent(); if ($token->getType() === Token::NEWLINE_TYPE && $tokens->look(-1)->getType() === Token::WHITESPACE_TYPE || $token->getType() === Token::TEXT_TYPE) { if (preg_match("/[[:blank:]]+\n/", $token->getValue())) { $this->addViolation($tokens->getFilename(), $token->getLine(), $token->getColumn(), 'A line should not end with blank space(s).'); } } $tokens->next(); } return $this->violations; }
protected function checkLoopUsageBody(Twig_TokenStream $stream, Twig_NodeInterface $node) { if ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name && 'loop' == $node->getNode('node')->getAttribute('name')) { $attribute = $node->getNode('attribute'); if ($attribute instanceof Twig_Node_Expression_Constant && in_array($attribute->getAttribute('value'), array('length', 'revindex0', 'revindex', 'last'))) { throw new Twig_Error_Syntax(sprintf('The "loop.%s" variable is not defined when looping with a condition', $attribute->getAttribute('value')), $node->getLine(), $stream->getFilename()); } } // should check for parent.loop.XXX usage if ($node instanceof Twig_Node_For) { return; } foreach ($node as $n) { if (!$n) { continue; } $this->checkLoopUsageBody($stream, $n); } }
/** * {@inheritdoc} */ public function check(\Twig_TokenStream $tokens) { $this->reset(); $variables = []; while (!$tokens->isEOF()) { $token = $tokens->getCurrent(); if ($token->getType() === \Twig_Token::NAME_TYPE) { if ($tokens->look(Lexer::PREVIOUS_TOKEN)->getType() === Token::WHITESPACE_TYPE && $tokens->look(-2)->getValue() === 'set') { $variables[$token->getValue()] = $token; } else { unset($variables[$token->getValue()]); } } $tokens->next(); } foreach ($variables as $name => $originalToken) { $this->addViolation($tokens->getFilename(), $originalToken->getLine(), $originalToken->getColumn(), sprintf('Unused variable "%s".', $name)); } return $this->violations; }
/** * @param \Twig_TokenStream $stream * @param int $lineno * @return WebpackInlineNode * @throws \Twig_Error_Syntax */ private function parseInline(\Twig_TokenStream $stream, $lineno) { if ($stream->test(\Twig_Token::NAME_TYPE)) { $stream->next(); } $stream->expect(\Twig_Token::BLOCK_END_TYPE); $this->parser->subparse(function ($token) { return $token->test(['end' . $this->getTag()]); }, true); $stream->expect(\Twig_Token::BLOCK_END_TYPE); $file = $this->parser->getEnvironment()->getLoader()->getCacheKey($stream->getFilename()); if (!isset($this->inline_blocks[$file])) { $this->inline_blocks[$file] = 0; } $file_name = md5($file . $this->inline_blocks[$file]) . '.js'; $assets = $this->extension->webpackAsset('cache.' . $file_name); $this->inline_blocks[$file]++; return new WebpackInlineNode(['js_file' => $assets['js'], 'css_file' => $assets['css']], $lineno, $this->getTag()); }
/** * @param \Twig_TokenStream $stream * * @throws \Twig_Error_Syntax */ private function throwSyntaxError(\Twig_TokenStream $stream) { $token = $stream->getCurrent(); throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $stream->getFilename()); }
/** * Helper method for the common operation of grabbing the next boolean value * from the stream * * @param \Twig_TokenStream $stream * @param string $optionName * @return string */ protected function getNextExpectedBoolValueFromStream(\Twig_TokenStream $stream, $optionName) { $stream->next(); $stream->expect(\Twig_Token::PUNCTUATION_TYPE); $expr = $this->parser->getExpressionParser()->parseExpression(); if (!$expr instanceof \Twig_Node_Expression_Constant || !is_bool($expr->getAttribute('value'))) { throw new SyntaxException(sprintf('The %s option must be boolean true or false (i.e. %s:false)', $optionName, $optionName), $stream->getCurrent()->getLine(), $stream->getFilename()); } return $expr->getAttribute('value'); }