FEATURE: Add FQObjectIdentifier to TypoScript tokens

* To make later checks easier, add the fully qualified object
  identifier, to all object identifier tokens while tokenizing
  TypoScript.
* Adjust tests accordingly.
* Add necessary autoloading.

Closes: #66
This commit is contained in:
Daniel Siepmann 2017-05-02 09:04:19 +02:00
parent 52b110c5a6
commit dffcac2bd5
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
10 changed files with 1405 additions and 70 deletions

View file

@ -16,7 +16,8 @@
},
"autoload": {
"psr-4": {
"Typo3Update\\": "src/Standards/Typo3Update/"
"Typo3Update\\": "src/Standards/Typo3Update/",
"Typo3Update\\CodeSniffer\\": "src/CodeSniffer/"
},
"files": [
"src/CodeSniffer/Tokenizers/TypoScript.php"

View file

@ -0,0 +1,125 @@
<?php
namespace Typo3Update\CodeSniffer\Tokenizers;
/*
* Copyright (C) 2017 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* Update tokens with fully qualified object identifier.
*/
class FQObjectIdentifier
{
/**
* Key used to save identifier to token.
* @var string
*/
const IDENTIFIER = 'fqObjectIdentifier';
/**
* The fully qualified object identifier, dot separated.
*
* @var array
*/
protected $fqPath = [];
/**
* Current "real" depth, count of opening braces.
* @var int
*/
protected $depth = 0;
/**
* Add token as path segment.
* @param array $token
*/
public function addPathSegment(array &$token)
{
$this->syncPath();
$path = [];
foreach (explode('.', $token['content']) as $pathSegment) {
$path[] = [
'content' => $pathSegment,
'depth' => $this->getDepth(),
];
}
$this->fqPath = array_merge($this->fqPath, $path);
$this->addFqObjectIdentifier($token);
}
/**
* Sync path with current depth.
*/
public function syncPath()
{
$this->fqPath = array_filter(
$this->fqPath,
function ($pathSegment) {
return $pathSegment['depth'] < $this->depth;
}
);
}
/**
* Respect opening brace internal.
*/
public function handleOpeningBrace()
{
++$this->depth;
}
/**
* Respect closing brace internal.
*/
public function handleClosingBrace()
{
--$this->depth;
$this->syncPath();
}
/**
* @return int
*/
public function getDepth()
{
return $this->depth;
}
/**
* @return string
*/
public function getPath()
{
$path = '';
foreach ($this->fqPath as $pathSegment) {
$path .= '.' . $pathSegment['content'];
}
return substr($path, 1);
}
/**
* @param array $token
*/
protected function addFqObjectIdentifier(array &$token)
{
$token[static::IDENTIFIER] = $this->getPath();
}
}

View file

@ -19,7 +19,9 @@
* 02110-1301, USA.
*/
use Helmich\TypoScriptParser\Tokenizer\TokenInterface;
use Helmich\TypoScriptParser\Tokenizer\Tokenizer;
use Typo3Update\CodeSniffer\Tokenizers\FQObjectIdentifier;
/**
* Tokenizes a string of TypoScript.
@ -68,8 +70,33 @@ class PHP_CodeSniffer_Tokenizers_TYPOSCRIPT
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter) We need to match the signature.
*/
public function processAdditional(&$tokens, $eolChar)
public function processAdditional(array &$tokens, $eolChar)
{
return;
$this->addFQObjectIdentifiers($tokens);
}
/**
* Add fully qualified object identifier to all object identifiers.
*
* @param array $tokens
*/
protected function addFQObjectIdentifiers(array &$tokens)
{
$fqObjectIdentifier = new FQObjectIdentifier();
foreach ($tokens as &$token) {
if ($token['type'] === TokenInterface::TYPE_OBJECT_IDENTIFIER) {
$fqObjectIdentifier->addPathSegment($token);
continue;
}
if ($token['type'] === TokenInterface::TYPE_BRACE_OPEN) {
$fqObjectIdentifier->handleOpeningBrace();
continue;
}
if ($token['type'] === TokenInterface::TYPE_BRACE_CLOSE) {
$fqObjectIdentifier->handleClosingBrace();
continue;
}
}
}
}

View file

@ -21,6 +21,7 @@
use Helmich\TypoScriptParser\Tokenizer\TokenInterface;
use PHP_CodeSniffer_File as PhpCsFile;
use Typo3Update\CodeSniffer\Tokenizers\FQObjectIdentifier;
use Typo3Update\Options;
use Typo3Update\Sniffs\Removed\AbstractGenericUsage;
@ -90,13 +91,19 @@ class Typo3Update_Sniffs_Removed_TypoScriptSniff extends AbstractGenericUsage
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
$objectIdentifier = $token['content'];
$objectIdentifiersToLoopUp = [$token['content']];
if (isset($this->configured[$objectIdentifier]) && $token['type'] === $this->configured[$objectIdentifier]['type']) {
$this->removed = [
$this->configured[$objectIdentifier]
];
return true;
if (isset($token[FQObjectIdentifier::IDENTIFIER]) && $token[FQObjectIdentifier::IDENTIFIER] !== $token['content']) {
$objectIdentifiersToLoopUp[] = $token[FQObjectIdentifier::IDENTIFIER];
}
foreach ($objectIdentifiersToLoopUp as $objectIdentifier) {
if (isset($this->configured[$objectIdentifier]) && $token['type'] === $this->configured[$objectIdentifier]['type']) {
$this->removed = [
$this->configured[$objectIdentifier]
];
return true;
}
}
return false;

View file

@ -0,0 +1,158 @@
<?php
namespace Typo3Update\Tests\CodeSniffer\Tokenizers;
/*
* Copyright (C) 2017 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use Helmich\TypoScriptParser\Tokenizer\TokenInterface;
use PHPUnit\Framework\TestCase;
use Typo3Update\CodeSniffer\Tokenizers\FQObjectIdentifier;
class FqObjectIdentifierTest extends TestCase
{
/**
* @test
*/
public function addingPathSegmentAddsFqToNewToken()
{
$initialToken = [
'type' => TokenInterface::TYPE_OBJECT_IDENTIFIER,
'content' => 'plugin.tx_example',
];
$lastToken = [
'type' => TokenInterface::TYPE_OBJECT_IDENTIFIER,
'content' => 'settings',
];
$expectedResult = $lastToken;
$expectedResult['fqObjectIdentifier'] = 'plugin.tx_example.settings';
$identifier = new FqObjectIdentifier();
$identifier->addPathSegment($initialToken);
$identifier->handleOpeningBrace();
$identifier->addPathSegment($lastToken);
$this->assertEquals(
$expectedResult,
$lastToken,
'Adding path segment does not add FQObjectIdentifier to token.'
);
}
/**
* @test
*/
public function addingPathSegment2ndTimeAddsFqToNewToken()
{
$initialToken = [
'type' => TokenInterface::TYPE_OBJECT_IDENTIFIER,
'content' => 'plugin.tx_example',
];
$firstToken = [
'type' => TokenInterface::TYPE_OBJECT_IDENTIFIER,
'content' => 'settings',
];
$lastToken = [
'type' => TokenInterface::TYPE_OBJECT_IDENTIFIER,
'content' => 'someSetting',
];
$expectedResult = $lastToken;
$expectedResult['fqObjectIdentifier'] = 'plugin.tx_example.settings.someSetting';
$identifier = new FqObjectIdentifier();
$identifier->addPathSegment($initialToken);
$identifier->handleOpeningBrace();
$identifier->addPathSegment($firstToken);
$identifier->handleOpeningBrace();
$identifier->addPathSegment($lastToken);
$this->assertEquals(
$expectedResult,
$lastToken,
'Adding path segment does not add FQObjectIdentifier to token on 2nd call.'
);
}
/**
* @test
*/
public function openingAndClosingBracesWillAdjustPath()
{
$initialToken = [
'type' => TokenInterface::TYPE_OBJECT_IDENTIFIER,
'content' => 'plugin.tx_example',
];
$firstToken = [
'type' => TokenInterface::TYPE_OBJECT_IDENTIFIER,
'content' => 'settings',
];
$secondToken = [
'type' => TokenInterface::TYPE_OBJECT_IDENTIFIER,
'content' => 'someSetting',
];
$lastToken = [
'type' => TokenInterface::TYPE_OBJECT_IDENTIFIER,
'content' => 'view',
];
$expectedResult = $lastToken;
$expectedResult['fqObjectIdentifier'] = 'plugin.tx_example.view';
$identifier = new FqObjectIdentifier();
$identifier->addPathSegment($initialToken);
$identifier->handleOpeningBrace();
$identifier->addPathSegment($firstToken);
$identifier->handleOpeningBrace();
$identifier->addPathSegment($secondToken);
$identifier->handleClosingBrace();
$identifier->addPathSegment($lastToken);
$this->assertEquals(
$expectedResult,
$lastToken,
'Curly braces do not modify path as expected.'
);
}
/**
* @test
*/
public function AddingPathSegmentAfterAnotherResetsPath()
{
$initialToken = [
'type' => TokenInterface::TYPE_OBJECT_IDENTIFIER,
'content' => 'plugin.tx_example.settings.someThing',
];
$lastToken = [
'type' => TokenInterface::TYPE_OBJECT_IDENTIFIER,
'content' => 'plugin.tx_example.settings.anotherOne',
];
$expectedResult = $lastToken;
$expectedResult['fqObjectIdentifier'] = $expectedResult['content'];
$identifier = new FqObjectIdentifier();
$identifier->addPathSegment($initialToken);
$identifier->addPathSegment($lastToken);
$this->assertEquals(
$expectedResult,
$lastToken,
'Adding path segment without braces in between resets.'
);
}
}

View file

@ -22,6 +22,8 @@ namespace Typo3Update\Tests\CodeSniffer\Tokenizers;
*/
use PHPUnit\Framework\TestCase;
use PHP_CodeSniffer_File as PhpCsFile;
use PHP_CodeSniffer as PhpCs;
/**
* Test TypoScript tokenizer.
@ -55,9 +57,12 @@ class TypoScriptTest extends TestCase
'example.ts',
]);
// Initialize constants, etc.
new PhpCs();
$this->assertEquals(
require $resultFile,
$subject->tokenizeString(file_get_contents($testFile), "\n"),
PhpCsFile::tokenizeString(file_get_contents($testFile), $subject, "\n"),
'Did not get expected tokens.'
);
}

View file

@ -3,4 +3,17 @@ plugin {
tx_example {
settings = TEST
}
tx_example_2 {
settings = TEST
}
}
plugin.tx_example.view.something = 1
plugin.tx_example {
view {
something = 1
}
settings2 = TEST
}
config {
enable_realurl = 1
}

File diff suppressed because it is too large Load diff

View file

@ -30,10 +30,19 @@
"source": "Typo3Update.Removed.TypoScript.styles-insertContent",
"type": "WARNING"
},
{
"column": 9,
"fixable": false,
"line": 14,
"message": "Legacy calls are not allowed; found mod.web_list.alternateBgColors. Removed in 7.0. Removed without substitution. See: https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.0/Breaking-53658-RemoveAlternateBgColorsOption.html",
"severity": 5,
"source": "Typo3Update.Removed.TypoScript.mod-web_list-alternateBgColors",
"type": "WARNING"
},
{
"column": 1,
"fixable": false,
"line": 13,
"line": 17,
"message": "Legacy calls are not allowed; found mod.web_list.alternateBgColors. Removed in 7.0. Removed without substitution. See: https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.0/Breaking-53658-RemoveAlternateBgColorsOption.html",
"severity": 5,
"source": "Typo3Update.Removed.TypoScript.mod-web_list-alternateBgColors",
@ -42,7 +51,7 @@
{
"column": 10,
"fixable": false,
"line": 26,
"line": 24,
"message": "Legacy calls are not allowed; found CLEARGIF. Removed in 7.1. Any installation should migrate to alternatives such as FLUIDTEMPLATE to customize the output of the content. See: https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.1/Breaking-64639-RemovedContentObjects.html",
"severity": 5,
"source": "Typo3Update.Removed.TypoScript.CLEARGIF",
@ -51,7 +60,7 @@
{
"column": 10,
"fixable": false,
"line": 27,
"line": 25,
"message": "Legacy calls are not allowed; found COLUMNS. Removed in 7.1. Any installation should migrate to alternatives such as FLUIDTEMPLATE to customize the output of the content. See: https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.1/Breaking-64639-RemovedContentObjects.html",
"severity": 5,
"source": "Typo3Update.Removed.TypoScript.COLUMNS",
@ -60,7 +69,7 @@
{
"column": 10,
"fixable": false,
"line": 28,
"line": 26,
"message": "Legacy calls are not allowed; found CTABLE. Removed in 7.1. Any installation should migrate to alternatives such as FLUIDTEMPLATE to customize the output of the content. See: https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.1/Breaking-64639-RemovedContentObjects.html",
"severity": 5,
"source": "Typo3Update.Removed.TypoScript.CTABLE",
@ -69,7 +78,7 @@
{
"column": 10,
"fixable": false,
"line": 29,
"line": 27,
"message": "Legacy calls are not allowed; found OTABLE. Removed in 7.1. Any installation should migrate to alternatives such as FLUIDTEMPLATE to customize the output of the content. See: https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.1/Breaking-64639-RemovedContentObjects.html",
"severity": 5,
"source": "Typo3Update.Removed.TypoScript.OTABLE",
@ -78,19 +87,19 @@
{
"column": 10,
"fixable": false,
"line": 30,
"line": 28,
"message": "Legacy calls are not allowed; found HRULER. Removed in 7.1. Any installation should migrate to alternatives such as FLUIDTEMPLATE to customize the output of the content. See: https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.1/Breaking-64639-RemovedContentObjects.html",
"severity": 5,
"source": "Typo3Update.Removed.TypoScript.HRULER",
"type": "WARNING"
}
],
"warnings": 9
"warnings": 10
}
},
"totals": {
"errors": 0,
"fixable": 0,
"warnings": 9
"warnings": 10
}
}

View file

@ -9,14 +9,12 @@ styles.insertContent {
}
}
# already covered
mod.web_list.alternateBgColors = 1
# Not covered yet
mod {
web_list {
alternateBgColors = 1
}
}
mod.web_list.alternateBgColors = 1
page {
CLEARGIF {