Validate declaration of methods
authorTim Düsterhus <duesterhus@woltlab.com>
Sat, 5 Jan 2013 16:05:11 +0000 (17:05 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Sat, 5 Jan 2013 16:13:22 +0000 (17:13 +0100)
CodeSniff/WCF/Sniffs/Methods/MethodDeclarationSniff.php [new file with mode: 0644]
CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php
CodeSniff/WCF/ruleset.xml
wcfsetup/install/files/lib/system/cronjob/HourlyCleanUpCronjob.class.php
wcfsetup/install/files/lib/system/database/Database.class.php
wcfsetup/install/files/lib/system/exception/NamedUserException.class.php
wcfsetup/install/files/lib/system/mail/SMTPMailSender.class.php
wcfsetup/install/files/lib/system/package/plugin/AbstractOptionPackageInstallationPlugin.class.php

diff --git a/CodeSniff/WCF/Sniffs/Methods/MethodDeclarationSniff.php b/CodeSniff/WCF/Sniffs/Methods/MethodDeclarationSniff.php
new file mode 100644 (file)
index 0000000..82eff15
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+/**
+ * This sniff is based on PSR2_Sniffs_Methods_MethodDeclarationSniff. Originally written
+ * by Greg Sherwood <gsherwood@squiz.net> and released under the terms of the BSD Licence.
+ * See: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/PSR2/Sniffs/Methods/MethodDeclarationSniff.php
+ *
+ * @author     Tim Duesterhus
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @category   Community Framework
+ */
+class WCF_Sniffs_Methods_MethodDeclarationSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff {
+       /**
+        * Constructs a Squiz_Sniffs_Scope_MethodScopeSniff.
+        */
+       public function __construct() {
+               parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION));
+       }
+       
+       /**
+        * Processes the function tokens within the class.
+        *
+        * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
+        * @param int                  $stackPtr  The position where the token was found.
+        * @param int                  $currScope The current scope opener token.
+        *
+        * @return void
+        */
+       protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
+       {
+               $tokens = $phpcsFile->getTokens();
+               
+               $methodName = $phpcsFile->getDeclarationName($stackPtr);
+               if ($methodName === null) {
+                       // Ignore closures.
+                       return;
+               }
+               
+               $visibility = 0;
+               $static = 0;
+               $abstract = 0;
+               $final = 0;
+               
+               $find = PHP_CodeSniffer_Tokens::$methodPrefixes;
+               $find[] = T_WHITESPACE;
+               $prev = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
+               
+               $prefix = $stackPtr;
+               while (($prefix = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$methodPrefixes, ($prefix - 1), $prev)) !== false) {
+                       switch ($tokens[$prefix]['code']) {
+                               case T_STATIC:
+                                       $static = $prefix;
+                                       break;
+                               case T_ABSTRACT:
+                                       $abstract = $prefix;
+                                       break;
+                               case T_FINAL:
+                                       $final = $prefix;
+                                       break;
+                               default:
+                                       $visibility = $prefix;
+                                       break;
+                       }
+               }
+               
+               if ($abstract > $visibility) {
+                       $error = 'The abstract declaration must precede the visibility declaration';
+                       $phpcsFile->addError($error, $abstract, 'AbstractAfterVisibility');
+               }
+               
+               if ($static !== 0 && $static < $visibility) {
+                       $error = 'The static declaration must come after the visibility declaration';
+                       $phpcsFile->addError($error, $static, 'StaticBeforeVisibility');
+               }
+               
+               if ($final !== 0 && ($final < $visibility || $final < $static)) {
+                       $error = 'The final declaration must come after the visibility declaration and after the static declaration';
+                       $phpcsFile->addError($error, $final, 'FinalBeforeVisibilityOrBeforeStatic');
+               }
+       }
+}
\ No newline at end of file
index 6a869d2406d3a0b3aa51304c4939216e444349a6..be5038a2c8b78035a19ddca5477cfaa15cf545d5 100644 (file)
@@ -7,7 +7,7 @@
  * @package    com.woltlab.wcf
  * @category   Community Framework
  */
-class WCF_Sniffs_Namespaces_ClassMustBeImportedSniff  implements PHP_CodeSniffer_Sniff {
+class WCF_Sniffs_Namespaces_ClassMustBeImportedSniff implements PHP_CodeSniffer_Sniff {
        /**
         * Returns an array of tokens this test wants to listen for.
         *
index 78532763776c52fd312507450a255886b9c16f73..ecfc6427385319c0eed7ecacb45e6e811af949ab 100644 (file)
@@ -6,6 +6,7 @@
        <exclude-pattern>*/CodeSniff/*</exclude-pattern>
        
        <rule ref="Generic.Classes.DuplicateClassName" />
+       <rule ref="Generic.CodeAnalysis.UselessOverridingMethod" />
        <rule ref="Generic.Files.ByteOrderMark" />
        <rule ref="Generic.Files.EndFileNewline" />
        <rule ref="Generic.Files.OneClassPerFile" />
@@ -20,7 +21,7 @@
        <rule ref="Generic.WhiteSpace.DisallowSpaceIndent" />
        <rule ref="Squiz.Arrays.ArrayBracketSpacing" />
        <rule ref="Squiz.Classes.LowercaseClassKeywords" />
-       <rule ref="Squiz.Classes.SelfMemberReference" /> <!-- TODO: Is this wanted? -->
+       <rule ref="Squiz.Classes.SelfMemberReference" />
        <rule ref="Squiz.Classes.ValidClassName" />
        <rule ref="Squiz.Commenting.DocCommentAlignment" />
        <rule ref="Squiz.ControlStructures.ElseIfDeclaration" />
        <rule ref="Squiz.ControlStructures.LowercaseDeclaration" />
        <rule ref="Squiz.Operators.ValidLogicalOperators" />
        <rule ref="Squiz.PHP.ForbiddenFunctions" />
+       <rule ref="Squiz.Scope.MethodScope" />
        <rule ref="WCF.Classes.ClassFileName" />
        <rule ref="WCF.ControlStructures.ControlSignature" />
-       <!--rule ref="WCF.PHP.DiscouragePCRE" /--> <!-- TODO: Sniff is disabled by commenting out the extends in the sniff file! -->
+       <rule ref="WCF.Methods.MethodDeclaration" />
        <rule ref="WCF.Namespaces.ClassMustBeImported" />
        <rule ref="WCF.Namespaces.SortedUseDeclaration" />
        <rule ref="WCF.Namespaces.UseDeclaration" />
+       <!--rule ref="WCF.PHP.DiscouragePCRE" /--> <!-- TODO: Sniff is disabled by commenting out the extends in the sniff file! -->
        <rule ref="WCF.WhiteSpace.SuperfluousWhitespace" />
        <rule ref="Zend.Files.ClosingTag" />
        
index c51948f13d9763b1767671c15ace6d4b169fa48a..dee30ce6a758c56941c472f68bb5cc5ec96a5aa9 100644 (file)
@@ -19,6 +19,7 @@ class HourlyCleanUpCronjob extends AbstractCronjob {
        public function execute(Cronjob $cronjob) {
                parent::execute($cronjob);
                
+               return;
                // TODO
        }
 }
index 98b4fc9813ee74b2bb662c6cbdb707b5413530ef..343c90ab96e1a6293a24a6832b4d30737a524309 100644 (file)
@@ -101,7 +101,7 @@ abstract class Database {
        /**
         * Connects to database server.
         */
-       public abstract function connect();
+       abstract public function connect();
        
        /**
         * Returns ID from last insert.
index 02ee09cf6e91ab2c50216e60b4320aae404ee08f..228c449135b49ec55982aebec83a51f13b23e35b 100644 (file)
@@ -13,15 +13,6 @@ use wcf\system\WCF;
  * @category   Community Framework
  */
 class NamedUserException extends UserException {
-       /**
-        * Creates a new NamedUserException object.
-        * 
-        * @param       string          $message
-        */
-       public function __construct($message) {
-               parent::__construct($message);
-       }
-       
        /**
         * Shows a styled page with the given error message.
         */
index f3555170ebc0183fd326cdd4d6c601d715670795..28bf840d8e40dcf8f706d348970b6193d847c779 100644 (file)
@@ -168,7 +168,7 @@ class SMTPMailSender extends MailSender {
        /**
         * Disconnects the Client-Server connection
         */
-       function disconnect() {
+       public function disconnect() {
                if ($this->connection === null) {
                        return;
                }
index 7e54970bbfff18d3e42a08ea71fbd806e52e8d57..6839a702680b7946526e8a9242e8136724506c41 100644 (file)
@@ -259,7 +259,7 @@ abstract class AbstractOptionPackageInstallationPlugin extends AbstractXMLPackag
         * @param       string          $categoryName
         * @param       integer         $existingOptionID
         */
-       protected abstract function saveOption($option, $categoryName, $existingOptionID = 0);
+       abstract protected function saveOption($option, $categoryName, $existingOptionID = 0);
        
        /**
         * @see wcf\system\package\plugin\AbstractXMLPackageInstallationPlugin::handleDelete()