Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a sniff to ensure multiple classes cannot be defined in a singl…

…e file
  • Loading branch information...
commit 37cf68ed7e9b925e04f3aeba4668c2abeefe58f0 1 parent d72d0a8
@davehauenstein authored
Showing with 74 additions and 0 deletions.
  1. +74 −0 Sniffs/Classes/MultipleClassesOneFileSniff.php
View
74 Sniffs/Classes/MultipleClassesOneFileSniff.php
@@ -0,0 +1,74 @@
+<?php
+
+/**
+ * Symfony2_Sniffs_Classes_MultipleClassesOneFileSniff.
+ *
+ * Throws errors if multiple classes are defined in a single file.
+ *
+ * Symfony coding standard specifies: "Define one class per file;"
+ *
+ * @author Dave Hauenstein <davehauenstein@gmail.com>
+ */
+class Symfony2_Sniffs_Classes_MultipleClassesOneFileSniff implements PHP_CodeSniffer_Sniff
+{
+ /**
+ * The number of times the T_CLASS token is encountered in the file.
+ *
+ * @var int
+ */
+ protected $_classCount = 0;
+
+ /**
+ * The current file this class is operating on.
+ *
+ * @var string
+ */
+ protected $_currentFile;
+
+ /**
+ * A list of tokenizers this sniff supports.
+ *
+ * @var array
+ */
+ public $supportedTokenizers = array(
+ 'PHP',
+ );
+
+ /**
+ * Returns an array of tokens this test wants to listen for.
+ *
+ * @return array
+ */
+ public function register()
+ {
+ return array(T_CLASS);
+ }
+
+ /**
+ * Processes this test, when one of its tokens is encountered.
+ *
+ * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
+ * @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)
+ {
+ if ($this->_currentFile !== $phpcsFile->getFilename()) {
+ $this->_classCount = 0;
+ $this->_currentFile = $phpcsFile->getFilename();
+ }
+
+ $this->_classCount++;
+
+ if ($this->_classCount > 1) {
+ $phpcsFile->addError(
+ 'Multiple classes defined in a single file',
+ $stackPtr
+ );
+ }
+
+ return;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.