*/ public function register() { return [\T_NAMESPACE]; } /** * Processes this test, when one of its tokens is encountered. * * @since 1.0.0 * * @param \PHP_CodeSniffer\Files\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(File $phpcsFile, $stackPtr) { $fileName = $phpcsFile->getFilename(); if ($this->currentFile !== $fileName) { // Reset the properties for each new file. $this->currentFile = $fileName; $this->declarationSeen = false; } if (Namespaces::isDeclaration($phpcsFile, $stackPtr) === false) { // Namespace operator, not a declaration; or live coding/parse error. return; } if ($this->declarationSeen === false) { // This is the first namespace declaration in the file. $this->declarationSeen = $stackPtr; return; } $tokens = $phpcsFile->getTokens(); // OK, so this is a file with multiple namespace declarations. $phpcsFile->addError( 'There should be only one namespace declaration per file. The first declaration was found on line %d', $stackPtr, 'MultipleFound', [$tokens[$this->declarationSeen]['line']] ); } }