From: Tim Düsterhus Date: Sat, 5 Jan 2013 16:05:11 +0000 (+0100) Subject: Validate declaration of methods X-Git-Tag: 2.0.0_Beta_1~587 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=81c5087acedaa84c442c4973b0acb5a894dcfbb9;p=GitHub%2FWoltLab%2FWCF.git Validate declaration of methods --- diff --git a/CodeSniff/WCF/Sniffs/Methods/MethodDeclarationSniff.php b/CodeSniff/WCF/Sniffs/Methods/MethodDeclarationSniff.php new file mode 100644 index 0000000000..82eff153c1 --- /dev/null +++ b/CodeSniff/WCF/Sniffs/Methods/MethodDeclarationSniff.php @@ -0,0 +1,81 @@ + 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 + * @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 diff --git a/CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php b/CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php index 6a869d2406..be5038a2c8 100644 --- a/CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php +++ b/CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php @@ -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. * diff --git a/CodeSniff/WCF/ruleset.xml b/CodeSniff/WCF/ruleset.xml index 7853276377..ecfc642738 100644 --- a/CodeSniff/WCF/ruleset.xml +++ b/CodeSniff/WCF/ruleset.xml @@ -6,6 +6,7 @@ */CodeSniff/* + @@ -20,7 +21,7 @@ - + @@ -29,12 +30,14 @@ + - + + diff --git a/wcfsetup/install/files/lib/system/cronjob/HourlyCleanUpCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/HourlyCleanUpCronjob.class.php index c51948f13d..dee30ce6a7 100644 --- a/wcfsetup/install/files/lib/system/cronjob/HourlyCleanUpCronjob.class.php +++ b/wcfsetup/install/files/lib/system/cronjob/HourlyCleanUpCronjob.class.php @@ -19,6 +19,7 @@ class HourlyCleanUpCronjob extends AbstractCronjob { public function execute(Cronjob $cronjob) { parent::execute($cronjob); + return; // TODO } } diff --git a/wcfsetup/install/files/lib/system/database/Database.class.php b/wcfsetup/install/files/lib/system/database/Database.class.php index 98b4fc9813..343c90ab96 100644 --- a/wcfsetup/install/files/lib/system/database/Database.class.php +++ b/wcfsetup/install/files/lib/system/database/Database.class.php @@ -101,7 +101,7 @@ abstract class Database { /** * Connects to database server. */ - public abstract function connect(); + abstract public function connect(); /** * Returns ID from last insert. diff --git a/wcfsetup/install/files/lib/system/exception/NamedUserException.class.php b/wcfsetup/install/files/lib/system/exception/NamedUserException.class.php index 02ee09cf6e..228c449135 100644 --- a/wcfsetup/install/files/lib/system/exception/NamedUserException.class.php +++ b/wcfsetup/install/files/lib/system/exception/NamedUserException.class.php @@ -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. */ diff --git a/wcfsetup/install/files/lib/system/mail/SMTPMailSender.class.php b/wcfsetup/install/files/lib/system/mail/SMTPMailSender.class.php index f3555170eb..28bf840d8e 100644 --- a/wcfsetup/install/files/lib/system/mail/SMTPMailSender.class.php +++ b/wcfsetup/install/files/lib/system/mail/SMTPMailSender.class.php @@ -168,7 +168,7 @@ class SMTPMailSender extends MailSender { /** * Disconnects the Client-Server connection */ - function disconnect() { + public function disconnect() { if ($this->connection === null) { return; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/AbstractOptionPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/AbstractOptionPackageInstallationPlugin.class.php index 7e54970bbf..6839a70268 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/AbstractOptionPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/AbstractOptionPackageInstallationPlugin.class.php @@ -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()