Permalink
Browse files

Added a few more sniffs and updated README

  • Loading branch information...
1 parent 601715a commit 06fe2b4324e71a39e17888e0a08e8ee0568fa6a6 @DASPRiD DASPRiD committed Jan 7, 2011
Showing with 277 additions and 0 deletions.
  1. +29 −0 README
  2. +60 −0 Sniffs/Files/ClosingTagSniff.php
  3. +179 −0 Sniffs/NamingConventions/ValidVariableNameSniff.php
  4. +9 −0 ruleset.xml
View
@@ -1 +1,30 @@
This is the PHPCS sniff for the Zend Framework 2 coding standard.
+
+It is currently not complete yet, as a few sniffs are still missing.
+
+DONE:
+
+- Check for proper function call argument style
+- Check for function braces being on a line by themself
+- Check for invalid usage for short open tags
+- Check for valid indentation
+- Check for character limit (80 soft, 120 hard)
+- Check for valid unix line-endings
+- Check for valid variable names
+- Check for invalid usage of closing PHP tag
+- Check for invalid usage of perl-style hash comments
+
+TODO:
+
+- Check for proper abstract class naming
+- Check for valid interface naming
+- Check for valid class/method naming
+- Check for valid constant naming
+- Check for proper user of string literals
+- Check for proper array styles
+- Check for proper class/method documentation blocks
+- Check for invalid use of 'var' keyword
+- Check for proper argument style in method declarations
+- Check for proper if statement usage
+- Check for proper switch usage
+
@@ -0,0 +1,60 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category Zend
+ * @package ZFCS
+ * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @version $Id$
+ */
+
+/**
+ * This sniff checks that files do not end with a closing tag.
+ *
+ * @category Zend
+ * @package ZFCS
+ * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class ZFCS_Sniffs_Files_ClosingTagSniff implements PHP_CodeSniffer_Sniff
+{
+ /**
+ * register(): defined by PHP_CodeSniffer_Sniff interface
+ *
+ * @see PHP_CodeSniffer_Sniff::register()
+ * @return array
+ */
+ public function register()
+ {
+ return array(T_CLOSE_TAG);
+ }
+
+ /**
+ * process(): defined by PHP_CodeSniffer_Sniff interface
+ *
+ * @see PHP_CodeSniffer_Sniff::process()
+ * @return void
+ */
+ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
+ {
+ $tokens = $phpcsFile->getTokens();
+ $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
+
+ if ($next === false) {
+ $error = 'A closing tag is not permitted at the end of a PHP file';
+ $phpcsFile->addError($error, $stackPtr, 'NotAllowed');
+ }
+ }
+}
+
@@ -0,0 +1,179 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category Zend
+ * @package ZFCS
+ * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @version $Id$
+ */
+
+/**
+ * This sniff checks for propper variable naming.
+ *
+ * All variables should be camel-cased and *should* not contain numbers.
+ *
+ * @category Zend
+ * @package ZFCS
+ * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class ZFCS_Sniffs_NamingConventions_ValidVariableNameSniff
+ extends PHP_CodeSniffer_Standards_AbstractVariableSniff
+{
+ /**
+ * Tokens to ignore so that we can find a DOUBLE_COLON.
+ *
+ * @var array
+ */
+ private $_ignore = array(
+ T_WHITESPACE,
+ T_COMMENT,
+ );
+
+ /**
+ * processVariable(): defined by PHP_CodeSniffer_Standards_AbstractVariableSniff class
+ *
+ * @see PHP_CodeSniffer_Sniff::processVariable()
+ * @param PHP_CodeSniffer_File $phpcsFile
+ * @param integer $stackPtr
+ * @return void
+ */
+ protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
+ {
+ $tokens = $phpcsFile->getTokens();
+ $varName = ltrim($tokens[$stackPtr]['content'], '$');
+
+ $phpReservedVars = array(
+ '_SERVER',
+ '_GET',
+ '_POST',
+ '_REQUEST',
+ '_SESSION',
+ '_ENV',
+ '_COOKIE',
+ '_FILES',
+ 'GLOBALS',
+ );
+
+ if (in_array($varName, $phpReservedVars) === true) {
+ return;
+ }
+
+ $objOperator = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
+
+ if ($tokens[$objOperator]['code'] === T_OBJECT_OPERATOR) {
+ // Check to see if we are using a variable from an object.
+ $var = $phpcsFile->findNext(array(T_WHITESPACE), ($objOperator + 1), null, true);
+
+ if ($tokens[$var]['code'] === T_STRING) {
+ // Either a var name or a function call, so check for bracket.
+ $bracket = $phpcsFile->findNext(array(T_WHITESPACE), ($var + 1), null, true);
+
+ if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) {
+ $objVarName = $tokens[$var]['content'];
+
+ if (PHP_CodeSniffer::isCamelCaps($objVarName, false, true, false) === false) {
+ $error = 'Variable "%s" is not in valid camel caps format';
+ $data = array($originalVarName);
+ $phpcsFile->addError($error, $var, 'NotCamelCaps', $data);
+ } else if (preg_match('|\d|', $objVarName)) {
+ $warning = 'Variable "%s" contains numbers but this is discouraged';
+ $data = array($originalVarName);
+ $phpcsFile->addWarning($warning, $stackPtr, 'ContainsNumbers', $data);
+ }
+ }
+ }
+ }
+
+ if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
+ $error = 'Variable "%s" is not in valid camel caps format';
+ $data = array($originalVarName);
+ $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
+ } else if (preg_match('|\d|', $varName)) {
+ $warning = 'Variable "%s" contains numbers but this is discouraged';
+ $data = array($originalVarName);
+ $phpcsFile->addWarning($warning, $stackPtr, 'ContainsNumbers', $data);
+ }
+ }
+
+ /**
+ * processMemberVar(): defined by PHP_CodeSniffer_Standards_AbstractVariableSniff class
+ *
+ * @see PHP_CodeSniffer_Sniff::processMemberVar()
+ * @param PHP_CodeSniffer_File $phpcsFile
+ * @param integer $stackPtr
+ * @return void
+ */
+ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
+ {
+ $tokens = $phpcsFile->getTokens();
+ $varName = ltrim($tokens[$stackPtr]['content'], '$');
+
+ if (PHP_CodeSniffer::isCamelCaps($varName, false, $public, false) === false) {
+ $error = 'Variable "%s" is not in valid camel caps format';
+ $data = array($varName);
+ $phpcsFile->addError($error, $stackPtr, 'MemberVarNotCamelCaps', $data);
+ } else if (preg_match('|\d|', $varName)) {
+ $warning = 'Variable "%s" contains numbers but this is discouraged';
+ $data = array($varName);
+ $phpcsFile->addWarning($warning, $stackPtr, 'MemberVarContainsNumbers', $data);
+ }
+ }
+
+ /**
+ * processVariableInString(): defined by PHP_CodeSniffer_Standards_AbstractVariableSniff class
+ *
+ * @see PHP_CodeSniffer_Sniff::processVariableInString()
+ * @param PHP_CodeSniffer_File $phpcsFile
+ * @param integer $stackPtr
+ * @return void
+ */
+ protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
+ {
+ $tokens = $phpcsFile->getTokens();
+
+ $phpReservedVars = array(
+ '_SERVER',
+ '_GET',
+ '_POST',
+ '_REQUEST',
+ '_SESSION',
+ '_ENV',
+ '_COOKIE',
+ '_FILES',
+ 'GLOBALS',
+ );
+
+ if (preg_match_all('|[^\\\]\$([a-zA-Z0-9_]+)|', $tokens[$stackPtr]['content'], $matches) !== 0) {
+ foreach ($matches[1] as $varName) {
+ if (in_array($varName, $phpReservedVars) === true) {
+ continue;
+ }
+
+ if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
+ $varName = $matches[0];
+ $error = 'Variable "%s" is not in valid camel caps format';
+ $data = array($originalVarName);
+ $phpcsFile->addError($error, $stackPtr, 'StringVarNotCamelCaps', $data);
+ } else if (preg_match('|\d|', $varName)) {
+ $warning = 'Variable "%s" contains numbers but this is discouraged';
+ $data = array($originalVarName);
+ $phpcsFile->addWarning($warning, $stackPtr, 'StringVarContainsNumbers', $data);
+ }
+ }
+ }
+ }
+}
+
View
@@ -2,19 +2,28 @@
<ruleset name="ZFCS">
<description>Zend Framework coding standard.</description>
+ <!-- Function call arguments must be separated by ', ' -->
<rule ref="Generic.Functions.FunctionCallArgumentSpacing"/>
+
+ <!-- Function braces must be on a line by themself -->
<rule ref="Generic.Functions.OpeningFunctionBraceBsdAllman"/>
+
+ <!-- Disallow short open tags -->
<rule ref="Generic.PHP.DisallowShortOpenTag"/>
+
+ <!-- Disallow tabs for indent, force 4 spaces -->
<rule ref="Generic.WhiteSpace.DisallowTabIndent"/>
<rule ref="Generic.WhiteSpace.ScopeIndentSniff"/>
+ <!-- Soft limit is 80 characters, hard limit is 120 -->
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="80"/>
<property name="absoluteLineLimit" value="120"/>
</properties>
</rule>
+ <!-- Only allow Unix-style line-endings -->
<rule ref="Generic.Files.LineEndings">
<properties>
<property name="eolChar" value="\n"/>

0 comments on commit 06fe2b4

Please sign in to comment.