Skip to content

Loading…

Add 'clone' and 'echo' to the list of known PHP statements to avoid the use of parenthesis. #63

Merged
merged 1 commit into from

2 participants

@elkuku
Joomla! member

Bad:

$foo = clone(FOO);
echo('foo');

Good:

$foo = clone FOO;
echo 'foo';
@dongilbert

Hrm, should this be here in the IncludingFilesSniff? Or create a new LanguageConstructsSniff or something like that?

@elkuku elkuku Add 'clone' and 'echo' to the list of known PHP statements to avoid t…
…he use of parenthesis.

Move the sniff to a proper file and remove obsolete code.
da7f45b
@elkuku
Joomla! member

It was out of laziness, but you are obviously right, so I moved the code to a proper sniff and removed the rest that had been disabled long ago.

@dongilbert dongilbert merged commit 04908e2 into joomla:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 1, 2014
  1. @elkuku

    Add 'clone' and 'echo' to the list of known PHP statements to avoid t…

    elkuku committed
    …he use of parenthesis.
    
    Move the sniff to a proper file and remove obsolete code.
Showing with 57 additions and 146 deletions.
  1. +0 −146 Sniffs/Files/IncludingFileSniff.php
  2. +57 −0 Sniffs/Functions/StatementNotFunctionSniff.php
View
146 Sniffs/Files/IncludingFileSniff.php
@@ -1,146 +0,0 @@
-<?php
-/**
- * Joomla_Sniffs_Files_IncludingFileSniff.
- *
- * PHP version 5
- *
- * @category PHP
- * @package PHP_CodeSniffer
- * @author Greg Sherwood <gsherwood@squiz.net>
- * @author Marc McIntyre <mmcintyre@squiz.net>
- * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
- * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
- * @version CVS: $Id: IncludingFileSniff.php 244643 2007-10-22 23:11:56Z squiz $
- * @link http://pear.php.net/package/PHP_CodeSniffer
- */
-
-/**
- * Joomla_Sniffs_Files_IncludingFileSniff.
- *
- * Checks that the include_once is used in conditional situations, and
- * require_once is used elsewhere. Also checks that brackets do not surround
- * the file being included.
- *
- * @category PHP
- * @package PHP_CodeSniffer
- * @author Greg Sherwood <gsherwood@squiz.net>
- * @author Marc McIntyre <mmcintyre@squiz.net>
- * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
- * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
- * @version Release: 1.3.0RC2
- * @link http://pear.php.net/package/PHP_CodeSniffer
- */
-class Joomla_Sniffs_Files_IncludingFileSniff implements PHP_CodeSniffer_Sniff
-{
-
- /**
- * Conditions that should use include_once
- *
- * @var array(int)
- */
- private static $_conditions = array(
- T_IF,
- T_ELSE,
- T_ELSEIF,
- T_SWITCH,
- );
-
-
- /**
- * Returns an array of tokens this test wants to listen for.
- *
- * @return array
- */
- public function register()
- {
- return array(
- T_INCLUDE_ONCE,
- T_REQUIRE_ONCE,
- T_REQUIRE,
- T_INCLUDE,
- );
-
- }//end register()
-
-
- /**
- * Processes this test, when one of its tokens is encountered.
- *
- * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
- * @param int $stackPtr The position of the current token in the
- * stack passed in $tokens.
- *
- * @return void
- */
- public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
- {
- $tokens = $phpcsFile->getTokens();
-
- $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
- if ($tokens[$nextToken]['code'] === T_OPEN_PARENTHESIS) {
- $error = '"%s" is a statement not a function; no parentheses are required';
- $data = array($tokens[$stackPtr]['content']);
- $phpcsFile->addError($error, $stackPtr, 'BracketsNotRequired', $data);
- }
-
- /*
- * elkuku: The test for conditional including has been disabled.
- */
-
- return;
-
- $inCondition = (count($tokens[$stackPtr]['conditions']) !== 0) ? true : false;
-
- // Check to see if this including statement is within the parenthesis
- // of a condition. If that's the case then we need to process it as being
- // within a condition, as they are checking the return value.
- if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
- foreach ($tokens[$stackPtr]['nested_parenthesis'] as $left => $right) {
- if (isset($tokens[$left]['parenthesis_owner']) === true) {
- $inCondition = true;
- }
- }
- }
-
- // Check to see if they are assigning the return value of this
- // including call. If they are then they are probably checking it, so
- // it's conditional.
- $previous = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
- if (in_array($tokens[$previous]['code'], PHP_CodeSniffer_Tokens::$assignmentTokens) === true) {
- // The have assigned the return value to it, so its conditional.
- $inCondition = true;
- }
-
- $tokenCode = $tokens[$stackPtr]['code'];
- if ($inCondition === true) {
- // We are inside a conditional statement. We need an include_once.
- if ($tokenCode === T_REQUIRE_ONCE) {
- $error = 'File is being conditionally included; ';
- $error .= 'use "include_once" instead';
- // AJE Change to warning.
- $phpcsFile->addWarning($error, $stackPtr, 'UseIncludeOnce');
- } else if ($tokenCode === T_REQUIRE) {
- $error = 'File is being conditionally included; ';
- $error .= 'use "include" instead';
- // AJE Change to warning.
- $phpcsFile->addWarning($error, $stackPtr, 'UseInclude');
- }
- } else {
- // We are unconditionally including, we need a require_once.
- if ($tokenCode === T_INCLUDE_ONCE) {
- $error = 'File is being unconditionally included; ';
- $error .= 'use "require_once" instead';
- $phpcsFile->addError($error, $stackPtr, 'UseRequireOnce');
- } else if ($tokenCode === T_INCLUDE) {
- $error = 'File is being unconditionally included; ';
- $error .= 'use "require" instead';
- $phpcsFile->addError($error, $stackPtr, 'UseRequire');
- }
- }//end if
-
- }//end process()
-
-
-}//end class
-
-?>
View
57 Sniffs/Functions/StatementNotFunctionSniff.php
@@ -0,0 +1,57 @@
+<?php
+/**
+ * Joomla_Sniffs_Functions_StatementNotFunctionSniff.
+ *
+ * @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE.txt
+ */
+
+/**
+ * Joomla_Sniffs_Functions_StatementNotFunctionSniff.
+ *
+ * Checks that language statements do no use brackets.
+ *
+ * @since 1.1
+ */
+class Joomla_Sniffs_Functions_StatementNotFunctionSniff implements PHP_CodeSniffer_Sniff
+{
+ /**
+ * Returns an array of tokens this test wants to listen for.
+ *
+ * @return array
+ */
+ public function register()
+ {
+ return array(
+ T_INCLUDE_ONCE,
+ T_REQUIRE_ONCE,
+ T_REQUIRE,
+ T_INCLUDE,
+ T_CLONE,
+ T_ECHO
+ );
+ }
+
+ /**
+ * Processes this test, when one of its tokens is encountered.
+ *
+ * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
+ * @param int $stackPtr The position of the current token in the
+ * stack passed in $tokens.
+ *
+ * @return void
+ */
+ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
+ {
+ $tokens = $phpcsFile->getTokens();
+
+ $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
+
+ if ($tokens[$nextToken]['code'] === T_OPEN_PARENTHESIS)
+ {
+ $error = '"%s" is a statement not a function; no parentheses are required';
+ $data = array($tokens[$stackPtr]['content']);
+ $phpcsFile->addError($error, $stackPtr, 'BracketsNotRequired', $data);
+ }
+ }
+}
Something went wrong with that request. Please try again.