Bug #11377

(Patch)Don't complain about inbuilt PHP/TYPO3 variables names

Added by demonkoryu over 2 years ago. Updated about 2 years ago.

Status:Resolved Start date:2010-12-10
Priority:Must have Due date:
Assignee:Andy Grunwald % Done:

100%

Category:NamingConventions Spent time: -
Target version:0.0.3
Branch: Tags:
Votes: 0

Description

Currently, variable names like $_SERVER, $TYPO3_CONF_VARS etc. are being complained about.

Slightly related, variables that are not lower camel case are not marked so if they also contain underscores.

Here is a revised version of ValidFunctionNameSniff.php:

    protected function processVariableNameCheck(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $scope = '') {
        $tokens = $phpcsFile->getTokens();
        $variableName = ltrim($tokens[$stackPtr]['content'], '$');
        $variableNamesWhitelistInbuilt = array('_SERVER', '_COOKIE', '_REQUEST', '_GET', '_POST', '_FILES', '_SESSION', '_ENV', 'GLOBALS');
        $variableNamesWhitelistTypo3 = array('TCA', '_EXTKEY', 'EM_CONF', 'TYPO3_CONF_VARS');
        $variableNamesWhitelist = array_merge($variableNamesWhitelistInbuilt, $variableNamesWhitelistTypo3);

        if (in_array($variableName, $variableNamesWhitelist)) {
            return;
        } else {
            $hasUnderscores = stripos($variableName, '_');
            $isLowerCamelCase = preg_match_all('/(\b[a-z]{1,})\b|(\b[a-z]{1,})([A-Z]{1}[a-z]{1,}){1,}\b/', $variableName, $matches);
            if ($hasUnderscores !== FALSE) {
                $error = 'Underscores are not allowed in the ' . $scope . 'variablename "$' . $variableName . '"; ';
                $error.= 'use lowerCamelCase for identifier instead';
                $phpcsFile->addError($error, $stackPtr);
            }
            if ($isLowerCamelCase === 0) {
                $error = ucfirst($scope) . 'variablename "$' . $variableName . '" must use lowerCamelCase;';
                $phpcsFile->addError($error, $stackPtr);
            }
        }

Related issues

related to PHP_CodeSniffer - Bug #12434: Add some NON_lower_camel_case variables to whitelist Closed 2011-01-21
blocked by PHP_CodeSniffer - Bug #12520: reopen: (Patch)Don't complain about inbuilt PHP/TYPO3 var... Closed 2010-12-10

Associated revisions

Revision 2363
Added by Stefano Kowalke over 2 years ago

Add an array with all reserved TYPO3 in-build variables. Resolves #11377 (Thanks to Timo Webler)

Revision 2363
Added by Stefano Kowalke over 2 years ago

Add an array with all reserved TYPO3 in-build variables. Resolves #11377 (Thanks to Timo Webler)

Revision f86e3ec4
Added by Stefano Kowalke over 2 years ago

Add an array with all reserved TYPO3 in-build variables. Resolves #11377 (Thanks to Timo Webler)

git-svn-id: https://svn.typo3.org/Teams/forge.typo3.org/hudson-helpers/tools/PHP_CodeSniffer/TYPO3/trunk@2363 54391d9a-7e4f-4783-837b-884f03fbcd44

History

Updated by Timo Webler over 2 years ago

Here a list of global variables (build in and TYPO3):
'_GET' and '_POST' are not in list, because the use of t3lib_div::_GET and t3lib_div::_Post is to be preferred.

array(
    'GLOBALS',
    'TYPO3_CONF_VARS',
    'TYPO3_LOADED_EXT',
    'TYPO3_DB',
    'EXEC_TIME',
    'SIM_EXEC_TIME',
    'TYPO_VERSION',
    'CLIENT',
    'PARSETIME_START',
    'PAGES_TYPES',
    'ICON_TYPES',
    'LANG_GENERAL_LABELS',
    'TCA',
    'TBE_MODULES',
    'TBE_STYLES',
    'T3_SERVICES',
    'T3_VAR',
    'FILEICONS',
    'WEBMOUNTS',
    'FILEMOUNTS',
    'BE_USER',
    'TBE_MODULES_EXT',
    'TCA_DESCR',
    '_EXTKEY',
    'EM_CONF',
    'LANG',
    'BACK_PATH',
    '_REQUEST',
    '_SERVER',
    '_REQUEST',
    '_COOKIE',
    '_FILES'
)

Updated by Stefano Kowalke over 2 years ago

  • Status changed from New to Accepted
  • Priority changed from Should have to Must have

Updated by Stefano Kowalke over 2 years ago

  • Status changed from Accepted to Resolved
  • Assignee set to Stefano Kowalke
  • Target version set to 0.0.3

Fixed in r2363.

Updated by Stefano Kowalke over 2 years ago

  • % Done changed from 0 to 100

Updated by Stefano Kowalke over 2 years ago

  • Status changed from Resolved to Closed

Updated by demonkoryu over 2 years ago

You forgot MCONF, MLANG.
This here: "Slightly related, variables that are not lower camel case are not marked so if they also contain underscores." also isn't fixed.
BTW thanks for not mentioning me in the changelog.

Updated by Andy Grunwald about 2 years ago

  • Status changed from Closed to Accepted
  • Assignee changed from Stefano Kowalke to Andy Grunwald

Hey,

i reopened this ticket, to work on it in th next few hours.

Updated by Stefano Kowalke about 2 years ago

  • Status changed from Accepted to Resolved

Also available in: Atom PDF