TASK: Refactor code base

* As method does not belong to class renaming, but is a general method,
  it was moved to a new trait.

Relates: #4
This commit is contained in:
Daniel Siepmann 2017-03-14 11:37:06 +01:00
parent 4134ba98f3
commit 20722f26d1
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
3 changed files with 71 additions and 40 deletions

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

@ -24,7 +24,7 @@ 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
{
@ -109,43 +109,4 @@ trait ClassnameCheckerTrait
{
return $classname;
}
/**
* 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

@ -27,6 +27,7 @@ use PHP_CodeSniffer_Tokens as Tokens;
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.