Merge pull request #17 from DanielSiepmann/feature/4-instantiation

Feature/4 instantiation
This commit is contained in:
Daniel Hürtgen 2017-03-16 14:40:13 +01:00 committed by GitHub
commit 1b2e72ec96
6 changed files with 344 additions and 1 deletions

View file

@ -56,6 +56,20 @@ new ones like ``\TYPO3\Extbase\...``. This is done for:
- Inline comments for IDEs, e.g. ``/* @var $configurationManager - Inline comments for IDEs, e.g. ``/* @var $configurationManager
Tx_Extbase_Configuration_ConfigurationManager */`` Tx_Extbase_Configuration_ConfigurationManager */``
- Instantiation through ``new``.
- Instantiation through ``makeInstance``. Only Classnames in Strings are supported, no ``::class``.
- Instantiation through ``ObjectManager``, check afterwards as this is static and all function calls
using ``get`` and ``create`` will be adjusted. Might be useful to exclude this sniff and run it
separately.
Only Classnames in Strings are supported, no ``::class``.
Also we check for the following deprecated calls:
- Check for ``create`` on ``ObjectManager``, which is "stupid" just all ``create`` calls are marked
with a warning.
What does it look like? What does it look like?
======================= =======================

View file

@ -0,0 +1,69 @@
<?php
namespace Typo3Update\Sniffs;
/*
* 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 PHP_CodeSniffer_Tokens as Tokens;
/**
* Provide common uses for all sniffs.
*/
trait ExtendedPhpCsSupportTrait
{
/**
* Check whether current stackPtr is a function call.
*
* Code taken from PEAR_Sniffs_Functions_FunctionCallSignatureSniff for reuse.
*
* @param PhpCsFile $phpcsFile
* @param int $stackPtr
*
* @return bool
*/
protected function isFunctionCall(PhpCsFile $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Find the next non-empty token.
$openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
// Not a function call.
return false;
}
if (isset($tokens[$openBracket]['parenthesis_closer']) === false) {
// Not a function call.
return false;
}
// Find the previous non-empty token.
$search = Tokens::$emptyTokens;
$search[] = T_BITWISE_AND;
$previous = $phpcsFile->findPrevious($search, ($stackPtr - 1), null, true);
if ($tokens[$previous]['code'] === T_FUNCTION) {
// It's a function definition, not a function call.
return false;
}
return true;
}
}

View file

@ -21,9 +21,10 @@ namespace Typo3Update\Sniffs\LegacyClassnames;
*/ */
use PHP_CodeSniffer_File as PhpcsFile; use PHP_CodeSniffer_File as PhpcsFile;
use PHP_CodeSniffer_Tokens as Tokens;
/** /**
* Provide common uses for all sniffs. * Provide common uses for all sniffs, regarding class name checks.
*/ */
trait ClassnameCheckerTrait trait ClassnameCheckerTrait
{ {

View file

@ -0,0 +1,98 @@
<?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.
*/
use PHP_CodeSniffer_Tokens as Tokens;
/**
* Detect and migrate static calls to old legacy classnames.
*/
class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithMakeInstanceSniff implements PHP_CodeSniffer_Sniff
{
use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait;
use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait;
/**
* Original token content for reuse accross methods.
* @var string
*/
protected $originalTokenContent = '';
/**
* Returns the token types that this sniff is interested in.
*
* @return array<int>
*/
public function register()
{
return Tokens::$functionNameTokens;
}
/**
* 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)
{
if (!$this->isFunctionCall($phpcsFile, $stackPtr)) {
return;
}
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['content'] !== 'makeInstance') {
return;
}
$classnamePosition = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $stackPtr);
if ($classnamePosition === false) {
return;
}
$classname = trim($tokens[$classnamePosition]['content'], '\'"');
$this->originalTokenContent = $tokens[$classnamePosition]['content'];
$this->addFixableError($phpcsFile, $classnamePosition, $classname);
}
/**
* As token contains more then just class name, we have to build new content ourself.
*
* @param string $classname
* @return string
*/
protected function getTokenForReplacement($classname)
{
$stringSign = $this->originalTokenContent[0];
$token = explode($stringSign, $this->originalTokenContent);
$token[1] = $classname;
// Migrate double quote to single quote.
// This way no escaping of backslashes in class names is necessary.
if ($stringSign === '"') {
$stringSign = "'";
}
return implode($stringSign, $token);
}
}

View file

@ -0,0 +1,59 @@
<?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.
*/
/**
* Detect and migrate static calls to old legacy classnames.
*/
class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithNewSniff implements PHP_CodeSniffer_Sniff
{
use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait;
/**
* Returns the token types that this sniff is interested in.
*
* @return array<int>
*/
public function register()
{
return [T_NEW];
}
/**
* 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);
if ($classnamePosition === false) {
return;
}
$classname = $tokens[$classnamePosition]['content'];
$this->addFixableError($phpcsFile, $classnamePosition, $classname);
}
}

View file

@ -0,0 +1,102 @@
<?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.
*/
use PHP_CodeSniffer_Tokens as Tokens;
/**
* Detect and migrate static calls to old legacy classnames.
*/
class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithObjectManagerSniff implements PHP_CodeSniffer_Sniff
{
use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait;
use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait;
/**
* Returns the token types that this sniff is interested in.
*
* @return array<int>
*/
public function register()
{
return Tokens::$functionNameTokens;
}
/**
* 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)
{
if (!$this->isFunctionCall($phpcsFile, $stackPtr)) {
return;
}
$tokens = $phpcsFile->getTokens();
$functionName = $tokens[$stackPtr]['content'];
if (!in_array($functionName, ['get', 'create'])) {
return;
}
$classnamePosition = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $stackPtr);
if ($classnamePosition === false) {
return;
}
if ($functionName === 'create') {
$phpcsFile->addWarning(
'The "create" method of ObjectManager is no longer supported, please migrate to "get".',
$stackPtr,
'mightBeDeprecatedMethod',
['create']
);
}
$classname = trim($tokens[$classnamePosition]['content'], '\'"');
$this->originalTokenContent = $tokens[$classnamePosition]['content'];
$this->addFixableError($phpcsFile, $classnamePosition, $classname);
}
/**
* As token contains more then just class name, we have to build new content ourself.
*
* @param string $classname
* @return string
*/
protected function getTokenForReplacement($classname)
{
$stringSign = $this->originalTokenContent[0];
$token = explode($stringSign, $this->originalTokenContent);
$token[1] = $classname;
// Migrate double quote to single quote.
// This way no escaping of backslashes in class names is necessary.
if ($stringSign === '"') {
$stringSign = "'";
}
return implode($stringSign, $token);
}
}