WIP|FEATURE: Integrate TypoScript

* Provide tokens for TypoScript.
* Provide first basic example of a working sniff.

Relates: #38
This commit is contained in:
Daniel Siepmann 2017-04-04 13:31:39 +02:00
parent 125c4280d6
commit 55bdb39fa5
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
3 changed files with 66 additions and 110 deletions

View file

@ -10,9 +10,15 @@
"src/CodeSniffer/Tokenizers/TypoScript.php" "src/CodeSniffer/Tokenizers/TypoScript.php"
] ]
}, },
"repositories": [
{
"type": "vcs",
"url": "https://github.com/DanielSiepmann/typo3-typoscript-parser"
}
],
"require": { "require": {
"squizlabs/php_codesniffer": "2.8.*", "squizlabs/php_codesniffer": "2.8.*",
"helmich/typo3-typoscript-parser": "1.0.*" "helmich/typo3-typoscript-parser": "dev-feature/allow-eol-handling"
}, },
"license": "GPL-2.0+", "license": "GPL-2.0+",
"authors": [ "authors": [

View file

@ -44,101 +44,15 @@ class PHP_CodeSniffer_Tokenizers_TYPOSCRIPT
*/ */
public function tokenizeString($string, $eolChar = '\n') public function tokenizeString($string, $eolChar = '\n')
{ {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t*** START TypoScript TOKENIZING ***" . PHP_EOL;
}
$tokenizer = new Tokenizer();
$tokens = $tokenizer->tokenizeString($string);
$finalTokens = []; $finalTokens = [];
$numTokens = count($tokens); $tokenizer = new Tokenizer($eolChar, false);
/** foreach ($tokenizer->tokenizeString($string) as $stackPtr => $token) {
* Each token within the stack contains information about itself:
*
* <code>
* array(
* 'code' => 301, // the token type code (see token_get_all())
* 'content' => 'if', // the token content
* 'type' => 'T_IF', // the token name
* 'line' => 56, // the line number when the token is located
* 'column' => 12, // the column in the line where this token
* // starts (starts from 1)
* 'level' => 2 // the depth a token is within the scopes open
* 'conditions' => array( // a list of scope condition token
* // positions => codes that
* 2 => 50, // opened the scopes that this token exists
* 9 => 353, // in (see conditional tokens section below)
* ),
* );
* </code>
*
* <b>Conditional Tokens</b>
*
* In addition to the standard token fields, conditions contain information to
* determine where their scope begins and ends:
*
* <code>
* array(
* 'scope_condition' => 38, // the token position of the condition
* 'scope_opener' => 41, // the token position that started the scope
* 'scope_closer' => 70, // the token position that ended the scope
* );
* </code>
*
* The condition, the scope opener and the scope closer each contain this
* information.
*
* <b>Parenthesis Tokens</b>
*
* Each parenthesis token (T_OPEN_PARENTHESIS and T_CLOSE_PARENTHESIS) has a
* reference to their opening and closing parenthesis, one being itself, the
* other being its opposite.
*
* <code>
* array(
* 'parenthesis_opener' => 34,
* 'parenthesis_closer' => 40,
* );
* </code>
*
* Some tokens can "own" a set of parenthesis. For example a T_FUNCTION token
* has parenthesis around its argument list. These tokens also have the
* parenthesis_opener and and parenthesis_closer indices. Not all parenthesis
* have owners, for example parenthesis used for arithmetic operations and
* function calls. The parenthesis tokens that have an owner have the following
* auxiliary array indices.
*
* <code>
* array(
* 'parenthesis_opener' => 34,
* 'parenthesis_closer' => 40,
* 'parenthesis_owner' => 33,
* );
* </code>
*
* Each token within a set of parenthesis also has an array index
* 'nested_parenthesis' which is an array of the
* left parenthesis => right parenthesis token positions.
*
* <code>
* 'nested_parenthesis' => array(
* 12 => 15
* 11 => 14
* );
* </code>
*/
$level = 0;
for ($stackPtr = 0; $stackPtr < $numTokens; $stackPtr++) {
$token = $tokens[$stackPtr];
$finalTokens[$stackPtr] = [ $finalTokens[$stackPtr] = [
'code' => $this->mapTypeToCode($token), 'code' => $token->getType(),
'type' => $token->getType(), 'type' => $token->getType(),
'line' => $token->getLine(), 'line' => $token->getLine(),
'column' => 0,
'content' => $token->getValue(), 'content' => $token->getValue(),
'level' => $level,
]; ];
} }
@ -157,23 +71,4 @@ class PHP_CodeSniffer_Tokenizers_TYPOSCRIPT
{ {
return; return;
} }
/**
* Returns mapped PHP code equivalent for token.
*
* @param Token $token
* @return int
*/
protected function mapTypeToCode(TokenInterface $token)
{
$tokenType = $token->getType();
$mapping = [
TokenInterface::TYPE_COMMENT_ONELINE => T_COMMENT,
];
if (!isset($mapping[$tokenType])) {
// TODO: Throw exception?!
}
return $mapping[$tokenType];
}
} }

View file

@ -0,0 +1,55 @@
<?php
use Helmich\TypoScriptParser\Tokenizer\TokenInterface;
use PHP_CodeSniffer_File as PhpCsFile;
use PHP_CodeSniffer_Sniff as PhpCsSniff;
class Typo3Update_Sniffs_TypoScript_ViewConfigurationSniff implements PhpCsSniff
{
public $supportedTokenizers = [
'TYPOSCRIPT',
];
/**
* Returns the token types that this sniff is interested in.
*
* @return array<int>
*/
public function register()
{
return [
TokenInterface::TYPE_OBJECT_IDENTIFIER,
];
}
/**
* Processes the tokens that this sniff is interested in.
*
* This is the default implementation, as most of the time next T_STRING is
* the class name. This way only the register method has to be registered
* in default cases.
*
* @param PhpCsFile $phpcsFile The file where the token was found.
* @param int $stackPtr The position in the stack where
* the token was found.
*
* @return void
*/
public function process(PhpCsFile $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
if ($token['content'] === 'layoutRootPath') {
$phpcsFile->addWarning(
'Do not use %s anymore, use %s instead.',
$stackPtr,
'legacy',
[
$token['content'],
'layoutRootPaths'
]
);
}
}
}