FEATURE: Provide first basic implementation of classmapping
* Install PHP CodeSniffer. * Install our project as new Standard. * Set our Standard as default. * Provide first basic implementation to detect implemented interfaces with old legacy class names and migrate them to new ones.
This commit is contained in:
parent
12e99db455
commit
f4fbbcab77
6 changed files with 190 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
/LegacyClassnames.php
|
||||
|
||||
/composer.lock
|
||||
/vendor/
|
16
Makefile
Normal file
16
Makefile
Normal file
|
@ -0,0 +1,16 @@
|
|||
BIN_PHPCS = ./vendor/bin/phpcs
|
||||
BIN_PHPCBF = ./vendor/bin/phpcbf
|
||||
DEFAULT_STANDARD = Typo3Update
|
||||
CUSTOM_STANDARDS = $(abspath ./src/Standards/)
|
||||
|
||||
install:
|
||||
composer install
|
||||
$(BIN_PHPCS) --config-set installed_paths $(CUSTOM_STANDARDS)
|
||||
$(BIN_PHPCS) -i | grep Typo3Update
|
||||
$(BIN_PHPCS) --config-set default_standard $(DEFAULT_STANDARD)
|
||||
|
||||
# For development / testing purposes:
|
||||
test-search:
|
||||
$(BIN_PHPCS) -p --colors -s PROJECT_PATH
|
||||
test-fix:
|
||||
$(BIN_PHPCBF) -p --colors -s PROJECT_PATH
|
27
Readme.rst
Normal file
27
Readme.rst
Normal file
|
@ -0,0 +1,27 @@
|
|||
.. _highlight: bash
|
||||
|
||||
About
|
||||
=====
|
||||
|
||||
Our goal is to provide an automated migration for TYPO3 updates.
|
||||
|
||||
This should include source code modifications like adjusting old legacy class names to new ones.
|
||||
|
||||
Requirements
|
||||
============
|
||||
|
||||
- ``composer`` needs to be installed and inside your ``$PATH``.
|
||||
|
||||
Installation
|
||||
============
|
||||
|
||||
Run::
|
||||
|
||||
make install
|
||||
|
||||
Copy the ``vendor/composer/autoload_classaliasmap.php`` generated by ``composer`` in your TYPO3 installation to ``LegacyClassnames.php`` in the root of this project.
|
||||
|
||||
Usage
|
||||
=====
|
||||
|
||||
Will follow.
|
23
composer.json
Normal file
23
composer.json
Normal file
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"name": "siepmann/typo3_update",
|
||||
"description": "Auto migrate PHP Source of extensions to be compatible.",
|
||||
"type": "project",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Typo3Update\\": "src/Classes/"
|
||||
}
|
||||
},
|
||||
"require-dev": {
|
||||
"psy/psysh": "~0.8"
|
||||
},
|
||||
"require": {
|
||||
"squizlabs/php_codesniffer_tests": "^2.0"
|
||||
},
|
||||
"license": "GPL-2.0+",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Daniel Siepmann",
|
||||
"email": "coding@daniel-siepmann.de"
|
||||
}
|
||||
]
|
||||
}
|
116
src/Standards/Typo3Update/Sniffs/Legacy/ClassnamesSniff.php
Normal file
116
src/Standards/Typo3Update/Sniffs/Legacy/ClassnamesSniff.php
Normal file
|
@ -0,0 +1,116 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This sniff detects old, legacy class names like t3lib_div.
|
||||
* Also it will make them fixable and migrate them to new ones.
|
||||
*/
|
||||
class Typo3Update_Sniffs_Legacy_ClassnamesSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
/**
|
||||
* Contains mapping from old -> new class names.
|
||||
* @var array
|
||||
*/
|
||||
protected $legacyClassnames = [];
|
||||
|
||||
/**
|
||||
* @param string $mappingFile File containing php array for mapping.
|
||||
*/
|
||||
public function __construct($mappingFile = __DIR__ . '/../../../../../LegacyClassnames.php')
|
||||
{
|
||||
$legacyClassnames = require $mappingFile;
|
||||
$this->legacyClassnames = $legacyClassnames['aliasToClassNameMapping'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the token types that this sniff is interested in.
|
||||
*
|
||||
* @see http://php.net/manual/en/tokens.php
|
||||
*
|
||||
* @return array(int)
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return [
|
||||
// T_USE,
|
||||
// T_NEW,
|
||||
// T_INSTANCEOF,
|
||||
T_IMPLEMENTS,
|
||||
// T_EXTENDS,
|
||||
// T_STRING,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the tokens that this sniff is interested in.
|
||||
*
|
||||
* @param PHP_CodeSniffer_File $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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
$classnamePosition = $phpcsFile->findNext(T_STRING, $stackPtr);
|
||||
$classname = $tokens[$classnamePosition]['content'];
|
||||
|
||||
if ($this->isLegacyClassname($classname)) {
|
||||
$fix = $phpcsFile->addFixableError(
|
||||
'Legacy classes are not allowed; found %s',
|
||||
$classnamePosition,
|
||||
'legacyClassname',
|
||||
[$classname]
|
||||
);
|
||||
|
||||
if ($fix === true) {
|
||||
switch ($tokens[$stackPtr]['code']) {
|
||||
case T_IMPLEMENTS:
|
||||
$phpcsFile->fixer->replaceToken($classnamePosition, $this->getNewClassname($classname));
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $classname
|
||||
* @return bool
|
||||
*/
|
||||
protected function isLegacyClassname($classname)
|
||||
{
|
||||
return isset($this->legacyClassnames[strtolower($classname)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $classname
|
||||
* @return string
|
||||
*/
|
||||
protected function getNewClassname($classname)
|
||||
{
|
||||
return $this->legacyClassnames[strtolower($classname)];
|
||||
}
|
||||
}
|
4
src/Standards/Typo3Update/ruleset.xml
Normal file
4
src/Standards/Typo3Update/ruleset.xml
Normal file
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0"?>
|
||||
<ruleset name="TYPO3 Update">
|
||||
<description>Provides sniffs and fixes for TYPO3 Updates.</description>
|
||||
</ruleset>
|
Loading…
Reference in a new issue