TASK: Refactor DRY

* Don't duplicate code and logic.
* Move common code to parent class.

Relates: #71
This commit is contained in:
Daniel Siepmann 2017-04-27 14:37:24 +02:00
parent 510774a3cc
commit 003609342e
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
3 changed files with 78 additions and 151 deletions

View file

@ -0,0 +1,72 @@
<?php
namespace Typo3Update;
/*
* 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 PHP_CodeSniffer_File as PhpCsFile;
use Typo3Update\RemovedByYamlConfiguration;
abstract class AbstractYamlRemovedUsage
{
protected $configured;
public function __construct()
{
$this->configured = new RemovedByYamlConfiguration(
$this->getRemovedConfigFiles(),
$this->getPrepareStructureCallback()
);
}
protected function getPrepareStructureCallback()
{
return function (array $typo3Versions) {
return call_user_func_array([$this, 'prepareStructure'], [$typo3Versions]);
};
}
abstract protected function prepareStructure(array $typo3Versions);
abstract protected function getRemovedConfigFiles();
protected function addWarning(PhpCsFile $phpcsFile, $stackPtr, array $removed)
{
$phpcsFile->addWarning(
'Calls to removed code are not allowed; found %s. Removed in %s. %s. See: %s',
$stackPtr,
$removed['identifier'],
[
$removed['oldUsage'],
$removed['versionRemoved'],
$this->getReplacement($removed),
$removed['docsUrl'],
]
);
}
protected function getReplacement(array $config)
{
$newCall = $config['replacement'];
if ($newCall !== null) {
return $newCall;
}
return 'There is no replacement, just remove call';
}
}

View file

@ -20,91 +20,13 @@ namespace Typo3Update\Feature;
* 02110-1301, USA. * 02110-1301, USA.
*/ */
use PHP_CodeSniffer as PhpCs;
use PHP_CodeSniffer_File as PhpCsFile;
use PHP_CodeSniffer_Sniff as PhpCsSniff; use PHP_CodeSniffer_Sniff as PhpCsSniff;
use Typo3Update\RemovedByYamlConfiguration; use Typo3Update\AbstractYamlRemovedUsage as BaseAbstractYamlRemovedUsage;
/** abstract class AbstractYamlRemovedUsage extends BaseAbstractYamlRemovedUsage implements FeatureInterface
* Contains common functionality for removed code like constants or functions.
*
* Removed parts are configured using YAML-Files, for examples see
* src/Standards/Typo3Update/Configuration/Removed/Constants/7.0.yaml Also
* check out the configuration options in Readme.rst.
*/
abstract class AbstractYamlRemovedUsage implements FeatureInterface
{ {
/**
* @var array
*/
protected $configured;
/**
* @var PhpCsSniff
*/
protected $sniff;
public function __construct(PhpCsSniff $sniff) public function __construct(PhpCsSniff $sniff)
{ {
$this->sniff = $sniff; parent::__construct();
$this->configured = new RemovedByYamlConfiguration(
$this->getRemovedConfigFiles(),
$this->getPrepateStructure()
);
}
protected function getPrepateStructure()
{
return function (array $typo3Versions) {
return call_user_func_array([$this, 'prepareStructure'], [$typo3Versions]);
};
}
/**
* Prepares structure from config for later usage.
*
* @param array $typo3Versions
* @return array
*/
abstract protected function prepareStructure(array $typo3Versions);
/**
* Return file names containing removed configurations.
*
* @return array<string>
*/
abstract protected function getRemovedConfigFiles();
protected function addWarning(PhpCsFile $phpcsFile, $stackPtr, array $removed)
{
$phpcsFile->addWarning(
'Calls to removed code are not allowed; found %s. Removed in %s. %s. See: %s',
$stackPtr,
$removed['identifier'],
[
$removed['oldUsage'],
$removed['versionRemoved'],
$this->getReplacement($removed),
$removed['docsUrl'],
]
);
}
/**
* The new call, or information how to migrate.
*
* To provide feedback for user to ease migration.
*
* @param array $config
*
* @return string
*/
protected function getReplacement(array $config)
{
$newCall = $config['replacement'];
if ($newCall !== null) {
return $newCall;
}
return 'There is no replacement, just remove call';
} }
} }

View file

@ -22,83 +22,16 @@ namespace Typo3Update\Sniffs\Removed;
use PHP_CodeSniffer_File as PhpCsFile; use PHP_CodeSniffer_File as PhpCsFile;
use PHP_CodeSniffer_Sniff as PhpCsSniff; use PHP_CodeSniffer_Sniff as PhpCsSniff;
use Typo3Update\RemovedByYamlConfiguration; use Typo3Update\AbstractYamlRemovedUsage as BaseAbstractYamlRemovedUsage;
/** abstract class AbstractGenericUsage extends BaseAbstractYamlRemovedUsage implements PhpCsSniff
* Contains common functionality for removed code like constants or functions.
*
* Removed parts are configured using YAML-Files, for examples see
* src/Standards/Typo3Update/Configuration/Removed/Constants/7.0.yaml Also
* check out the configuration options in Readme.rst.
*/
abstract class AbstractGenericUsage implements PhpCsSniff
{ {
protected $configured;
public function __construct()
{
$this->configured = new RemovedByYamlConfiguration(
$this->getRemovedConfigFiles(),
$this->getPrepateStructure()
);
}
protected function getPrepateStructure()
{
return function (array $typo3Versions) {
return call_user_func_array([$this, 'prepareStructure'], [$typo3Versions]);
};
}
/**
* Prepares structure from config for later usage.
*
* @param array $typo3Versions
* @return array
*/
abstract protected function prepareStructure(array $typo3Versions);
/**
* Return file names containing removed configurations.
*
* @return array<string>
*/
abstract protected function getRemovedConfigFiles();
abstract protected function findRemoved(PhpCsFile $phpcsFile, $stackPtr); abstract protected function findRemoved(PhpCsFile $phpcsFile, $stackPtr);
public function process(PhpCsFile $phpcsFile, $stackPtr) public function process(PhpCsFile $phpcsFile, $stackPtr)
{ {
foreach ($this->findRemoved($phpcsFile, $stackPtr) as $removed) { foreach ($this->findRemoved($phpcsFile, $stackPtr) as $removed) {
$phpcsFile->addWarning( $this->addWarning($phpcsFile, $stackPtr, $removed);
'Calls to removed code are not allowed; found %s. Removed in %s. %s. See: %s',
$stackPtr,
$removed['identifier'],
[
$removed['oldUsage'],
$removed['versionRemoved'],
$this->getReplacement($removed),
$removed['docsUrl'],
]
);
} }
} }
/**
* The new call, or information how to migrate.
*
* To provide feedback for user to ease migration.
*
* @param array $config
*
* @return string
*/
protected function getReplacement(array $config)
{
$newCall = $config['replacement'];
if ($newCall !== null) {
return $newCall;
}
return 'There is no replacement, just remove call';
}
} }