/** * 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) { // We want all the errors from the PEAR standard, plus some of our own. parent::process($phpcsFile, $stackPtr); $this->processOpen($phpcsFile, $stackPtr); $this->processClose($phpcsFile, $stackPtr); }
/** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param integer $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(); $errorData = array($tokens[$stackPtr]['content']); // braces $openingBrace = $tokens[$stackPtr]['scope_opener']; $closingBrace = $tokens[$stackPtr]['scope_closer']; // look for empty bodies $body = $phpcsFile->getTokensAsString($openingBrace, $closingBrace - $openingBrace + 1); if (preg_match('/^\\{\\s*\\}$/', $body)) { if (preg_match('/\\s/', $body)) { $error = 'Opening and closing braces of an empty %s must be on the same line as the definition, with no spaces between them.'; $phpcsFile->addError($error, $stackPtr, 'EmptyBodyBraces', $errorData); } // body has content } else { parent::process($phpcsFile, $stackPtr); // check braces alignment if ($tokens[$openingBrace]['column'] !== $tokens[$closingBrace]['column']) { $error = 'Closing braces must have the same indentation than their respective opening one.'; $phpcsFile->addError($error, $closingBrace, 'BadClosingBraceIndentation', $errorData); } } }
/** * 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) { // We want all the errors from the PEAR standard, plus some of our own. parent::process($phpcsFile, $stackPtr); // Just in case. $tokens = $phpcsFile->getTokens(); if (isset($tokens[$stackPtr]['scope_opener']) === false) { return; } $this->processOpen($phpcsFile, $stackPtr); $this->processClose($phpcsFile, $stackPtr); }
/** * 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) { // We want all the errors from the PEAR standard, plus some of our own. parent::process($phpcsFile, $stackPtr); $tokens = $phpcsFile->getTokens(); /* Check that this is the only class or interface in the file. */ $nextClass = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), $stackPtr + 1); if ($nextClass !== false) { // We have another, so an error is thrown. $error = 'Only one interface or class is allowed in a file'; $phpcsFile->addError($error, $nextClass, 'MultipleClasses'); } /* Check alignment of the keyword and braces. */ if ($tokens[$stackPtr - 1]['code'] === T_WHITESPACE) { $prevContent = $tokens[$stackPtr - 1]['content']; if ($prevContent !== $phpcsFile->eolChar) { $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar)); $spaces = strlen($blankSpace); if (in_array($tokens[$stackPtr - 2]['code'], array(T_ABSTRACT, T_FINAL)) === false) { if ($spaces !== 0) { $type = strtolower($tokens[$stackPtr]['content']); $error = 'Expected 0 spaces before %s keyword; %s found'; $data = array($type, $spaces); $phpcsFile->addError($error, $stackPtr, 'SpaceBeforeKeyword', $data); } } else { if ($spaces !== 1) { $type = strtolower($tokens[$stackPtr]['content']); $prevContent = strtolower($tokens[$stackPtr - 2]['content']); $error = 'Expected 1 space between %s and %s keywords; %s found'; $data = array($prevContent, $type, $spaces); $phpcsFile->addError($error, $stackPtr, 'SpacesBeforeKeyword', $data); } } } } //end if if (isset($tokens[$stackPtr]['scope_opener']) === false) { $error = 'Possible parse error: %s missing opening or closing brace'; $data = array($tokens[$stackPtr]['content']); $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data); return; } $closeBrace = $tokens[$stackPtr]['scope_closer']; if ($tokens[$closeBrace - 1]['code'] === T_WHITESPACE) { $prevContent = $tokens[$closeBrace - 1]['content']; if ($prevContent !== $phpcsFile->eolChar) { $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar)); $spaces = strlen($blankSpace); if ($spaces !== 0) { if ($tokens[$closeBrace - 1]['line'] !== $tokens[$closeBrace]['line']) { $error = 'Expected 0 spaces before closing brace; newline found'; $phpcsFile->addError($error, $closeBrace, 'NewLineBeforeCloseBrace'); } else { $error = 'Expected 0 spaces before closing brace; %s found'; $data = array($spaces); $phpcsFile->addError($error, $closeBrace, 'SpaceBeforeCloseBrace', $data); } } } } // Check that the closing brace has one blank line after it. $nextContent = $phpcsFile->findNext(array(T_WHITESPACE, T_COMMENT), $closeBrace + 1, null, true); if ($nextContent === false) { // No content found, so we reached the end of the file. // That means there was no closing tag either. $error = 'Closing brace of a %s must be followed by a blank line and then a closing PHP tag'; $data = array($tokens[$stackPtr]['content']); $phpcsFile->addError($error, $closeBrace, 'EndFileAfterCloseBrace', $data); } else { $nextLine = $tokens[$nextContent]['line']; $braceLine = $tokens[$closeBrace]['line']; if ($braceLine === $nextLine) { $error = 'Closing brace of a %s must be followed by a single blank line'; $data = array($tokens[$stackPtr]['content']); $phpcsFile->addError($error, $closeBrace, 'NoNewlineAfterCloseBrace', $data); } else { if ($nextLine !== $braceLine + 2) { $difference = $nextLine - $braceLine - 1; $error = 'Closing brace of a %s must be followed by a single blank line; found %s'; $data = array($tokens[$stackPtr]['content'], $difference); $phpcsFile->addError($error, $closeBrace, 'NewlinesAfterCloseBrace', $data); } } } //end if // Check the closing brace is on it's own line, but allow // for comments like "//end class". $nextContent = $phpcsFile->findNext(T_COMMENT, $closeBrace + 1, null, true); if ($tokens[$nextContent]['content'] !== $phpcsFile->eolChar && $tokens[$nextContent]['line'] === $tokens[$closeBrace]['line']) { $type = strtolower($tokens[$stackPtr]['content']); $error = 'Closing %s brace must be on a line by itself'; $data = array($tokens[$stackPtr]['content']); $phpcsFile->addError($error, $closeBrace, 'CloseBraceSameLine', $data); } /* Check that each of the parent classes or interfaces specified are spaced correctly. */ // We need to map out each of the possible tokens in the declaration. $keyword = $stackPtr; $openingBrace = $tokens[$stackPtr]['scope_opener']; $className = $phpcsFile->findNext(T_STRING, $stackPtr); /* Now check the spacing of each token. */ $name = strtolower($tokens[$keyword]['content']); // Spacing of the keyword. $gap = $tokens[$stackPtr + 1]['content']; if (strlen($gap) !== 1) { $found = strlen($gap); $error = 'Expected 1 space between %s keyword and %s name; %s found'; $data = array($name, $name, $found); $phpcsFile->addError($error, $stackPtr, 'SpaceAfterKeyword', $data); } // Check after the name. $gap = $tokens[$className + 1]['content']; if (strlen($gap) !== 1) { $found = strlen($gap); $error = 'Expected 1 space after %s name; %s found'; $data = array($name, $found); $phpcsFile->addError($error, $stackPtr, 'SpaceAfterName', $data); } // Now check each of the parents. $parents = array(); $nextParent = $className + 1; while (($nextParent = $phpcsFile->findNext(array(T_STRING, T_IMPLEMENTS), $nextParent + 1, $openingBrace - 1)) !== false) { $parents[] = $nextParent; } $parentCount = count($parents); for ($i = 0; $i < $parentCount; $i++) { if ($tokens[$parents[$i]]['code'] === T_IMPLEMENTS) { continue; } if ($tokens[$parents[$i] - 1]['code'] === T_COMMA || $tokens[$parents[$i] - 1]['code'] === T_NS_SEPARATOR && $tokens[$parents[$i] - 2]['code'] === T_COMMA) { $name = $tokens[$parents[$i]]['content']; $error = 'Expected 1 space before "%s"; 0 found'; $data = array($name); $phpcsFile->addError($error, $nextComma + 1, 'NoSpaceBeforeName', $data); } else { $spaceBefore = strlen($tokens[$parents[$i] - 1]['content']); if ($spaceBefore !== 1) { $name = $tokens[$parents[$i]]['content']; $error = 'Expected 1 space before "%s"; %s found'; $data = array($name, $spaceBefore); $phpcsFile->addError($error, $stackPtr, 'SpaceBeforeName', $data); } } if ($tokens[$parents[$i] + 1]['code'] !== T_COMMA) { if ($i !== $parentCount - 1) { // This is not the last parent, and the comma // is not where we expect it to be. if ($tokens[$parents[$i] + 2]['code'] !== T_IMPLEMENTS) { $found = strlen($tokens[$parents[$i] + 1]['content']); $name = $tokens[$parents[$i]]['content']; $error = 'Expected 0 spaces between "%s" and comma; $%s found'; $data = array($name, $found); $phpcsFile->addError($error, $stackPtr, 'SpaceBeforeComma', $data); } } $nextComma = $phpcsFile->findNext(T_COMMA, $parents[$i]); } else { $nextComma = $parents[$i] + 1; } } //end for }
/** * 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) { // We want all the errors from the PEAR standard, plus some of our own. parent::process($phpcsFile, $stackPtr); $tokens = $phpcsFile->getTokens(); /* Check that this is the only class or interface in the file. */ $nextClass = $phpcsFile->findNext(array(T_CLASS), $stackPtr + 1); if ($nextClass !== false) { // We have another, so an error is thrown. $error = '[Clases#archivo] Incluir solo una clase por archivo'; $phpcsFile->addError($error, $nextClass); } /* Check alignment of the keyword and braces. */ if ($tokens[$stackPtr - 1]['code'] === T_WHITESPACE) { $prevContent = $tokens[$stackPtr - 1]['content']; if ($prevContent !== $phpcsFile->eolChar) { $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar)); $spaces = strlen($blankSpace); if (in_array($tokens[$stackPtr - 2]['code'], array(T_ABSTRACT, T_FINAL)) === false) { if ($spaces !== 0) { $type = strtolower($tokens[$stackPtr]['content']); $error = "[Clases#declaracion] No se esparaban espacios antes de {$type}; se encontraron {$spaces}"; $phpcsFile->addError($error, $stackPtr); } } else { if ($spaces !== 1) { $type = strtolower($tokens[$stackPtr]['content']); $prevContent = strtolower($tokens[$stackPtr - 2]['content']); $error = "[Clases#declaracion] Se esperaba 1 espacio entre {$prevContent} y {$type}; Se encontraron {$spaces}"; $phpcsFile->addError($error, $stackPtr); } } } } //end if if (isset($tokens[$stackPtr]['scope_opener']) === false) { $error = '[Clases#declaracion] Possible parse error: '; $error .= $tokens[$stackPtr]['content']; $error .= ' missing opening or closing brace'; $phpcsFile->addWarning($error, $stackPtr); return; } $closeBrace = $tokens[$stackPtr]['scope_closer']; /*if ($tokens[($closeBrace - 1)]['code'] === T_WHITESPACE) { $prevContent = $tokens[($closeBrace - 1)]['content']; if ($prevContent !== $phpcsFile->eolChar) { $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar)); $spaces = strlen($blankSpace); if ($spaces !== 0) { $error = "[Clases#declaracion] No se esperaban espacios antes de la llave de cierre; se encontraron $spaces"; $phpcsFile->addError($error, $closeBrace); } } }*/ // Check that the closing brace has one blank line after it. $nextContent = $phpcsFile->findNext(array(T_WHITESPACE, T_COMMENT), $closeBrace + 1, null, true); if ($nextContent === false) { // No content found, so we reached the end of the file. // That means there was no closing tag either. $error = '[Clases#declaracion] La llave de cierre '; $error .= $tokens[$stackPtr]['content']; $error .= ' debe ser seguido de una línea en blanco y luego el tag de cierre de PHP'; $phpcsFile->addError($error, $closeBrace); } else { /* $nextLine = $tokens[$nextContent]['line']; $braceLine = $tokens[$closeBrace]['line']; if ($braceLine === $nextLine) { $error = '[Clases#declaracion] Closing brace of a '; $error .= $tokens[$stackPtr]['content']; $error .= ' must be followed by a single blank line'; $phpcsFile->addError($error, $closeBrace); } else if ($nextLine !== ($braceLine + 2)) { $difference = ($nextLine - $braceLine - 1).' lines'; $error = '[Clases#declaracion] Closing brace of a '; $error .= $tokens[$stackPtr]['content']; $error .= ' must be followed by a single blank line; found '.$difference; $phpcsFile->addError($error, $closeBrace); } */ } //end if // Check the closing brace is on it's own line, but allow // for comments like "//end class". $nextContent = $phpcsFile->findNext(T_COMMENT, $closeBrace + 1, null, true); if ($tokens[$nextContent]['content'] !== $phpcsFile->eolChar && $tokens[$nextContent]['line'] === $tokens[$closeBrace]['line']) { $type = strtolower($tokens[$stackPtr]['content']); $error = "[Clases#declaracion] Closing {$type} brace must be on a line by itself"; $phpcsFile->addError($error, $closeBrace); } /* Check that each of the parent classes or interfaces specified are spaced correctly. */ // We need to map out each of the possible tokens in the declaration. $keyword = $stackPtr; $openingBrace = $tokens[$stackPtr]['scope_opener']; $className = $phpcsFile->findNext(T_STRING, $stackPtr); /* Now check the spacing of each token. */ $name = strtolower($tokens[$keyword]['content']); // Spacing of the keyword. $gap = $tokens[$stackPtr + 1]['content']; if (strlen($gap) !== 1) { $found = strlen($gap); $error = "[Clases#declaracion] Expected 1 space between {$name} keyword and {$name} name; {$found} found"; $phpcsFile->addError($error, $stackPtr); } // Check after the name. /* $gap = $tokens[($className + 1)]['content']; if (strlen($gap) !== 1) { $found = strlen($gap); $error = "[Clases#declaracion] Expected 1 space after $name name; $found found"; $phpcsFile->addError($error, $stackPtr); }*/ // Now check each of the parents. $parents = array(); $nextParent = $className + 1; while (($nextParent = $phpcsFile->findNext(array(T_STRING, T_IMPLEMENTS), $nextParent + 1, $openingBrace - 1)) !== false) { $parents[] = $nextParent; } $parentCount = count($parents); for ($i = 0; $i < $parentCount; $i++) { if ($tokens[$parents[$i]]['code'] === T_IMPLEMENTS) { continue; } if ($tokens[$parents[$i] - 1]['code'] !== T_WHITESPACE) { $name = $tokens[$parents[$i]]['content']; $error = "[Clases#declaracion] Expected 1 space before \"{$name}\"; 0 found"; $phpcsFile->addError($error, $nextComma + 1); } else { $spaceBefore = strlen($tokens[$parents[$i] - 1]['content']); if ($spaceBefore !== 1) { $name = $tokens[$parents[$i]]['content']; $error = "[Clases#declaracion] Expected 1 space before \"{$name}\"; {$spaceBefore} found"; $phpcsFile->addError($error, $stackPtr); } } if ($tokens[$parents[$i] + 1]['code'] !== T_COMMA) { if ($i !== $parentCount - 1) { // This is not the last parent, and the comma // is not where we expect it to be. if ($tokens[$parents[$i] + 2]['code'] !== T_IMPLEMENTS) { $found = strlen($tokens[$parents[$i] + 1]['content']); $name = $tokens[$parents[$i]]['content']; $error = "[Clases#declaracion] Expected 0 spaces between \"{$name}\" and comma; {$found} found"; $phpcsFile->addError($error, $stackPtr); } } $nextComma = $phpcsFile->findNext(T_COMMA, $parents[$i]); } else { $nextComma = $parents[$i] + 1; } } //end for }