Example #1
0
 /**
  * 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)
 {
     $tokens = $phpcsfile->gettokens();
     $firstcontent = $phpcsfile->findnext(array(T_WHITESPACE), $stackptr + 1, null, true);
     // If the first non-whitespace token is not an opening parenthesis, then we are not concerned.
     if ($tokens[$firstcontent]['code'] !== T_OPEN_PARENTHESIS) {
         return;
     }
     $endofstatement = $phpcsfile->findnext(array(T_SEMICOLON), $stackptr, null, false);
     // If the token before the semi-colon is not a closing parenthesis, then we are not concerned.
     if ($tokens[$endofstatement - 1]['code'] !== T_CLOSE_PARENTHESIS) {
         return;
     }
     if ($phpcsfile->findnext(PHP_CodeSniffer_tokens::$operators, $stackptr, $endofstatement, false) === false) {
         // There are no arithmetic operators in this.
         $error = 'Echoed strings should not be bracketed';
         $phpcsfile->adderror($error, $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)
 {
     $tokens = $phpcsfile->gettokens();
     // Find the next non-empty token.
     $next = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, $stackptr + 1, null, true);
     if ($tokens[$next]['code'] !== T_OPEN_PARENTHESIS) {
         // Not a function call.
         return;
     }
     if (isset($tokens[$next]['parenthesis_closer']) === false) {
         // Not a function call.
         return;
     }
     // Find the previous non-empty token.
     $previous = $phpcsfile->findprevious(PHP_CodeSniffer_tokens::$emptyTokens, $stackptr - 1, null, true);
     if ($tokens[$previous]['code'] === T_FUNCTION) {
         // It's a function definition, not a function call.
         return;
     }
     if ($tokens[$previous]['code'] === T_NEW) {
         // We are creating an object, not calling a function.
         return;
     }
     if ($stackptr + 1 !== $next) {
         // Checking this: $value = my_function[*](...).
         $error = 'Space before opening parenthesis of function call prohibited';
         $phpcsfile->adderror($error, $stackptr);
     }
     if ($tokens[$next + 1]['code'] === T_WHITESPACE) {
         // Checking this: $value = my_function([*]...).
         $error = 'Space after opening parenthesis of function call prohibited';
         $phpcsfile->adderror($error, $stackptr);
     }
     $closer = $tokens[$next]['parenthesis_closer'];
     if ($tokens[$closer - 1]['code'] === T_WHITESPACE) {
         // Checking this: $value = my_function(...[*]).
         $between = $phpcsfile->findnext(T_WHITESPACE, $next + 1, null, true);
         // Only throw an error if there is some content between the parenthesis.
         // IE. Checking for this: $value = my_function().
         // If there is no content, then we would have thrown an error in the
         // previous IF statement because it would look like this:
         // $value = my_function( ).
         if ($between !== $closer) {
             $error = 'Space before closing parenthesis of function call prohibited';
             $phpcsfile->adderror($error, $closer);
         }
     }
     $next = $phpcsfile->findnext(T_WHITESPACE, $closer + 1, null, true);
     if ($tokens[$next]['code'] === T_SEMICOLON) {
         if (in_array($tokens[$closer + 1]['code'], PHP_CodeSniffer_tokens::$emptyTokens) === true) {
             $error = 'Space after closing parenthesis of function call prohibited';
             $phpcsfile->adderror($error, $closer);
         }
     }
 }
Example #3
0
 /**
  * Processes this test, when one of its tokens is encountered.
  *
  * @param PHP_CodeSniffer_File $phpcsfile The current file being processed.
  * @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)
 {
     $tokens = $phpcsfile->gettokens();
     $classname = $phpcsfile->findnext(T_STRING, $stackptr);
     $name = trim($tokens[$classname]['content']);
     // Make sure that the word is all lowercase
     if (!preg_match('/[a-z]?/', $name)) {
         $error = ucfirst($tokens[$stackptr]['content']) . ' name is not valid, must be all lower-case';
         $phpcsfile->adderror($error, $stackptr);
     }
 }
 /**
  * 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)
 {
     $tokens = $phpcsfile->gettokens();
     // If this is an inline condition (ie. there is no scope opener), then
     // return, as this is not a new scope.
     if (isset($tokens[$stackptr]['scope_closer']) === false) {
         return;
     }
     // We need to actually find the first piece of content on this line,
     // as if this is a method with tokens before it (public, static etc)
     // or an if with an else before it, then we need to start the scope
     // checking from there, rather than the current token.
     $linestart = $stackptr - 1;
     for ($linestart; $linestart > 0; $linestart--) {
         if (strpos($tokens[$linestart]['content'], $phpcsfile->eolChar) !== false) {
             break;
         }
     }
     // We found a new line, now go forward and find the first non-whitespace
     // token.
     $linestart = $phpcsfile->findnext(array(T_WHITESPACE), $linestart + 1, null, true);
     $startcolumn = $tokens[$linestart]['column'];
     $scopestart = $tokens[$stackptr]['scope_opener'];
     $scopeend = $tokens[$stackptr]['scope_closer'];
     // Check that the closing brace is on its own line.
     $lastcontent = $phpcsfile->findprevious(array(T_WHITESPACE), $scopeend - 1, $scopestart, true);
     if ($tokens[$lastcontent]['line'] === $tokens[$scopeend]['line']) {
         $error = 'Closing brace must be on a line by itself';
         $phpcsfile->adderror($error, $scopeend);
         return;
     }
     // Check now that the closing brace is lined up correctly.
     $braceindent = $tokens[$scopeend]['column'];
     $isbreakcloser = $tokens[$scopeend]['code'] === T_BREAK;
     if (in_array($tokens[$stackptr]['code'], array(T_CASE, T_DEFAULT)) === true && $isbreakcloser === true) {
         // BREAK statements should be indented 4 spaces from the
         // CASE or DEFAULT statement.
         if ($braceindent !== $startcolumn + 4) {
             $error = 'Break statement indented incorrectly; expected ' . ($startcolumn + 3) . ' spaces, found ' . ($braceindent - 1);
             $phpcsfile->adderror($error, $scopeend);
         }
     } else {
         if (in_array($tokens[$stackptr]['code'], array(T_CASE, T_DEFAULT))) {
             $startcolumn -= 4;
         }
         if ($braceindent !== $startcolumn) {
             $error = 'Closing brace indented incorrectly; expected ' . ($startcolumn - 1) . ' spaces, found ' . ($braceindent - 1);
             $phpcsfile->adderror($error, $scopeend);
         }
     }
 }
 /**
  * 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)
 {
     $tokens = $phpcsfile->gettokens();
     if ($tokens[$stackptr]['code'] !== T_STRING) {
         $content = $tokens[$stackptr]['content'];
         if ($content !== strtolower($content)) {
             $type = strtoupper($content);
             $expected = strtolower($content);
             $error = "{$type} keyword must be lowercase; expected \"{$expected}\" but found \"{$content}\"";
             $phpcsfile->adderror($error, $stackptr);
         }
         return;
     }
     // Make sure this is a function call.
     $next = $phpcsfile->findnext(T_WHITESPACE, $stackptr + 1, null, true);
     if ($next === false) {
         // Not a function call.
         return;
     }
     if ($tokens[$next]['code'] !== T_OPEN_PARENTHESIS) {
         // Not a function call.
         return;
     }
     $prev = $phpcsfile->findprevious(T_WHITESPACE, $stackptr - 1, null, true);
     if ($tokens[$prev]['code'] === T_FUNCTION) {
         // Function declaration, not a function call.
         return;
     }
     if ($tokens[$prev]['code'] === T_OBJECT_OPERATOR) {
         // Not an inbuilt function.
         return;
     }
     if ($tokens[$prev]['code'] === T_DOUBLE_COLON) {
         // Not an inbuilt function.
         return;
     }
     // Make sure it is an inbuilt PHP function.
     // PHP_CodeSniffer doesn't include/require any files, so no
     // user defined global functions can exist, except for
     // PHP_CodeSniffer ones.
     $content = $tokens[$stackptr]['content'];
     if (function_exists($content) === false) {
         return;
     }
     if ($content !== strtolower($content)) {
         $expected = strtolower($content);
         $error = "Calls to inbuilt PHP functions must be lowercase; expected \"{$expected}\" but found \"{$content}\"";
         $phpcsfile->adderror($error, $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)
 {
     // If short open tags are off, then any short open tags will be converted
     // to inline_html tags so we can just ignore them.
     // If its on, then we want to ban the use of them.
     $option = ini_get('short_open_tag');
     // Ini_get returns a string "0" if short open tags is off.
     if ($option === '0') {
         return;
     }
     $tokens = $phpcsfile->gettokens();
     $opentag = $tokens[$stackptr];
     if ($opentag['content'] === '<?') {
         $error = 'Short PHP opening tag used. Found "' . $opentag['content'] . '" Expected "<?php".';
         $phpcsfile->adderror($error, $stackptr);
     }
     if ($opentag['code'] === T_OPEN_TAG_WITH_ECHO) {
         $nextvar = $tokens[$phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, $stackptr + 1, null, true)];
         $error = 'Short PHP opening tag used with echo. Found "';
         $error .= $opentag['content'] . ' ' . $nextvar['content'] . ' ..." but expected "<?php echo ' . $nextvar['content'] . ' ...".';
         $phpcsfile->adderror($error, $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)
 {
     $tokens = $phpcsfile->gettokens();
     $token = $tokens[$stackptr];
     // Skip function without body.
     if (isset($token['scope_opener']) === false) {
         return;
     }
     // Get function name.
     $methodname = $phpcsfile->getdeclarationname($stackptr);
     // Get all parameters from method signature.
     $signature = array();
     foreach ($phpcsfile->getmethodparameters($stackptr) as $param) {
         $signature[] = $param['name'];
     }
     $next = ++$token['scope_opener'];
     $end = --$token['scope_closer'];
     for (; $next <= $end; ++$next) {
         $code = $tokens[$next]['code'];
         if (in_array($code, PHP_CodeSniffer_tokens::$emptyTokens) === true) {
             continue;
         } else {
             if ($code === T_RETURN) {
                 continue;
             }
         }
         break;
     }
     // Any token except 'parent' indicates correct code.
     if ($tokens[$next]['code'] !== T_PARENT) {
         return;
     }
     // Find next non empty token index, should be double colon.
     $next = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, $next + 1, null, true);
     // Skip for invalid code.
     if ($next === false || $tokens[$next]['code'] !== T_DOUBLE_COLON) {
         return;
     }
     // Find next non empty token index, should be the function name.
     $next = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, $next + 1, null, true);
     // Skip for invalid code or other method.
     if ($next === false || $tokens[$next]['content'] !== $methodname) {
         return;
     }
     // Find next non empty token index, should be the open parenthesis.
     $next = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, $next + 1, null, true);
     // Skip for invalid code.
     if ($next === false || $tokens[$next]['code'] !== T_OPEN_PARENTHESIS) {
         return;
     }
     $validparametertypes = array(T_VARIABLE, T_LNUMBER, T_CONSTANT_ENCAPSED_STRING);
     $parameters = array('');
     $parenthesiscount = 1;
     $count = count($tokens);
     for (++$next; $next < $count; ++$next) {
         $code = $tokens[$next]['code'];
         if ($code === T_OPEN_PARENTHESIS) {
             ++$parenthesiscount;
         } else {
             if ($code === T_CLOSE_PARENTHESIS) {
                 --$parenthesiscount;
             } else {
                 if ($parenthesiscount === 1 && $code === T_COMMA) {
                     $parameters[] = '';
                 } else {
                     if (in_array($code, PHP_CodeSniffer_tokens::$emptyTokens) === false) {
                         $parameters[count($parameters) - 1] .= $tokens[$next]['content'];
                     }
                 }
             }
         }
         if ($parenthesiscount === 0) {
             break;
         }
     }
     $next = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, $next + 1, null, true);
     if ($next === false || $tokens[$next]['code'] !== T_SEMICOLON) {
         return;
     }
     // Check rest of the scope.
     for (++$next; $next <= $end; ++$next) {
         $code = $tokens[$next]['code'];
         // Skip for any other content.
         if (in_array($code, PHP_CodeSniffer_tokens::$emptyTokens) === false) {
             return;
         }
     }
     $parameters = array_map('trim', $parameters);
     $parameters = array_filter($parameters);
     if (count($parameters) === count($signature) && $parameters === $signature) {
         $phpcsfile->addwarning('Useless method overriding detected', $stackptr);
     }
 }
Example #8
0
    /**
     * 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)
    {
        $this->currentfile = $phpcsfile;
        // We are only interested if this is the first open tag.
        if ($stackptr !== 0) {
            if ($phpcsfile->findprevious(T_OPEN_TAG, $stackptr - 1) !== false) {
                return;
            }
        }
        $tokens = $phpcsfile->gettokens();
        // Find the next non whitespace token.
        $commentstart = $phpcsfile->findnext(T_WHITESPACE, $stackptr + 1, null, true);
        // Look for $Id$ and boilerplate
        if ($tokens[$commentstart]['code'] != T_COMMENT) {
            $phpcsfile->adderror('File must begin with License boilerplate', $stackptr + 1);
            return;
        } else {
            if (preg_match('|\\$Id|i', $tokens[$commentstart]['content'])) {
                $phpcsfile->addwarning("\$Id\$ tag is no longer required, please remove.", $stackptr + 1);
                return;
            }
        }
        // now look for boilerplate, must be immediately after the first line
        $boilerplate = '// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.';
        $nexttoken = $phpcsfile->findnext(T_WHITESPACE, $stackptr + 1, null, true);
        $boilerplate_lines = preg_split('/[\\n\\r]+/', $boilerplate);
        if (rtrim($tokens[$nexttoken]['content']) != $boilerplate_lines[0]) {
            $phpcsfile->adderror('You must include the Moodle boilerplate at the top of the file', $nexttoken);
            return;
        }
        $boilerplate_index = 0;
        foreach ($boilerplate_lines as $line) {
            $nexttoken = $phpcsfile->findnext(T_COMMENT, $nexttoken);
            if (rtrim($tokens[$nexttoken]['content']) != $boilerplate_lines[$boilerplate_index]) {
                $phpcsfile->adderror('Badly formatted boilerplate. Please copy-paste exactly', $nexttoken);
                return;
            }
            $nexttoken++;
            $boilerplate_index++;
        }
        $filedoctoken = $phpcsfile->findnext(T_WHITESPACE, $nexttoken + 1, null, true);
        if ($tokens[$filedoctoken]['code'] === T_CLOSE_TAG) {
            // We are only interested if this is the first open tag.
            return;
        } else {
            if ($tokens[$filedoctoken]['code'] === T_COMMENT) {
                $phpcsfile->adderror('You must use "/**" style comments for a file comment', $filedoctoken + 1);
                return;
            } else {
                if ($filedoctoken === false || $tokens[$filedoctoken]['code'] !== T_DOC_COMMENT) {
                    $phpcsfile->adderror('Missing file doc comment', $filedoctoken + 1);
                    return;
                } else {
                    // Extract the header comment docblock.
                    $commentend = $phpcsfile->findnext(T_DOC_COMMENT, $filedoctoken + 1, null, true) - 1;
                    // Check if there is only 1 doc comment between the open tag and class token.
                    $nexttoken = array(T_ABSTRACT, T_CLASS, T_FUNCTION, T_DOC_COMMENT);
                    $commentnext = $phpcsfile->findnext($nexttoken, $commentend + 1);
                    if ($commentnext !== false && $tokens[$commentnext]['code'] !== T_DOC_COMMENT) {
                        // Found a class token right after comment doc block.
                        $newlinetoken = $phpcsfile->findnext(T_WHITESPACE, $commentend + 1, $commentnext, false, $phpcsfile->eolChar);
                        if ($newlinetoken !== false) {
                            $newlinetoken = $phpcsfile->findnext(T_WHITESPACE, $newlinetoken + 1, $commentnext, false, $phpcsfile->eolChar);
                            if ($newlinetoken === false) {
                                // No blank line between the class token and the doc block.
                                // The doc block is most likely a class comment.
                                $phpcsfile->adderror('Missing file doc comment', $stackptr + 1);
                                return;
                            }
                        }
                    }
                    $comment = $phpcsfile->gettokensAsString($filedoctoken, $commentend - $filedoctoken + 1);
                    // Parse the header comment docblock.
                    try {
                        $this->commentparser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsfile);
                        $this->commentparser->parse();
                    } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
                        $line = $e->getlinewithinComment() + $filedoctoken;
                        $phpcsfile->adderror($e->getMessage(), $line);
                        return;
                    }
                    $comment = $this->commentparser->getComment();
                    if (is_null($comment) === true) {
                        $error = 'File doc comment is empty';
                        $phpcsfile->adderror($error, $filedoctoken);
                        return;
                    }
                    // No extra newline before short description.
                    $short = $comment->getShortComment();
                    $newlinecount = 0;
                    $newlinespan = strspn($short, $phpcsfile->eolChar);
                    if ($short !== '' && $newlinespan > 0) {
                        $line = $newlinespan > 1 ? 'newlines' : 'newline';
                        $error = "Extra {$line} found before file comment short description";
                        $phpcsfile->adderror($error, $filedoctoken + 1);
                    }
                    $newlinecount = substr_count($short, $phpcsfile->eolChar) + 1;
                    // Exactly one blank line between short and long description.
                    $long = $comment->getlongcomment();
                    if (empty($long) === false) {
                        $between = $comment->getWhiteSpacebetween();
                        $newlinebetween = substr_count($between, $phpcsfile->eolChar);
                        if ($newlinebetween !== 2) {
                            $error = 'There should be exactly one blank line between descriptions in file comment';
                            $phpcsfile->addwarning($error, $filedoctoken + $newlinecount + 1);
                        }
                        $newlinecount += $newlinebetween;
                    }
                    // Exactly one blank line before tags.
                    $tags = $this->commentparser->gettagOrders();
                    if (count($tags) > 1) {
                        $newlinespan = $comment->getNewlineAfter();
                        if ($newlinespan !== 2) {
                            $error = 'There must be exactly one blank line before the tags in file comment';
                            if ($long !== '') {
                                $newlinecount += substr_count($long, $phpcsfile->eolChar) - $newlinespan + 1;
                            }
                            $phpcsfile->addwarning($error, $filedoctoken + $newlinecount);
                            $short = rtrim($short, $phpcsfile->eolChar . ' ');
                        }
                    }
                    // Check the PHP Version.
                    /*
                    if (strstr(strtolower($long), 'php version') === false) {
                        $error = 'PHP version not specified';
                        $phpcsfile->addwarning($error, $commentend);
                    }
                    */
                    // Check each tag.
                    $this->processtags($filedoctoken, $commentend);
                }
            }
        }
    }
Example #9
0
 /**
  * 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)
 {
     // Modify array of required tags
     $this->tags['package']['required'] = false;
     $this->tags['copyright']['required'] = false;
     $this->tags['author']['required'] = false;
     $this->currentfile = $phpcsfile;
     $tokens = $phpcsfile->gettokens();
     $type = strtolower($tokens[$stackptr]['content']);
     $find = array(T_ABSTRACT, T_WHITESPACE, T_FINAL);
     // Extract the class comment docblock.
     $commentend = $phpcsfile->findprevious($find, $stackptr - 1, null, true);
     if ($commentend !== false && $tokens[$commentend]['code'] === T_COMMENT) {
         $phpcsfile->adderror("You must use \"/**\" style comments for a {$type} comment", $stackptr);
         return;
     } else {
         if ($commentend === false || $tokens[$commentend]['code'] !== T_DOC_COMMENT) {
             $phpcsfile->adderror("Missing {$type} doc comment", $stackptr);
             return;
         }
     }
     $commentstart = $phpcsfile->findprevious(T_DOC_COMMENT, $commentend - 1, null, true) + 1;
     $commentnext = $phpcsfile->findprevious(T_WHITESPACE, $commentend + 1, $stackptr, false, $phpcsfile->eolChar);
     // Distinguish file and class comment.
     $prevclasstoken = $phpcsfile->findprevious(T_CLASS, $stackptr - 1);
     if ($prevclasstoken === false) {
         // This is the first class token in this file, need extra checks.
         $prevnoncomment = $phpcsfile->findprevious(T_DOC_COMMENT, $commentstart - 1, null, true);
         if ($prevnoncomment !== false) {
             $prevcomment = $phpcsfile->findprevious(T_DOC_COMMENT, $prevnoncomment - 1);
             if ($prevcomment === false) {
                 // There is only 1 doc comment between open tag and class token.
                 $newlinetoken = $phpcsfile->findnext(T_WHITESPACE, $commentend + 1, $stackptr, false, $phpcsfile->eolChar);
                 if ($newlinetoken !== false) {
                     $newlinetoken = $phpcsfile->findnext(T_WHITESPACE, $newlinetoken + 1, $stackptr, false, $phpcsfile->eolChar);
                     if ($newlinetoken !== false) {
                         // Blank line between the class and the doc block.
                         // The doc block is most likely a file comment.
                         $phpcsfile->adderror("Missing {$type} doc comment", $stackptr + 1);
                         return;
                     }
                 }
             }
         }
     }
     $comment = $phpcsfile->gettokensAsString($commentstart, $commentend - $commentstart + 1);
     // Parse the class comment.docblock.
     try {
         $this->commentparser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsfile);
         $this->commentparser->parse();
     } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
         $line = $e->getlinewithinComment() + $commentstart;
         $phpcsfile->adderror($e->getMessage(), $line);
         return;
     }
     $comment = $this->commentparser->getComment();
     if (is_null($comment) === true) {
         $error = ucfirst($type) . ' doc comment is empty';
         $phpcsfile->adderror($error, $commentstart);
         return;
     }
     // No extra newline before short description.
     $short = $comment->getShortComment();
     $newlinecount = 0;
     $newlinespan = strspn($short, $phpcsfile->eolChar);
     if ($short !== '' && $newlinespan > 0) {
         $line = $newlinespan > 1 ? 'newlines' : 'newline';
         $error = "Extra {$line} found before {$type} comment short description";
         $phpcsfile->adderror($error, $commentstart + 1);
     }
     $newlinecount = substr_count($short, $phpcsfile->eolChar) + 1;
     // Exactly one blank line between short and long description.
     $long = $comment->getlongcomment();
     if (empty($long) === false) {
         $between = $comment->getWhiteSpacebetween();
         $newlinebetween = substr_count($between, $phpcsfile->eolChar);
         if ($newlinebetween !== 2) {
             $error = "There must be exactly one blank line between descriptions in {$type} comments";
             $phpcsfile->adderror($error, $commentstart + $newlinecount + 1);
         }
         $newlinecount += $newlinebetween;
     }
     // Exactly one blank line before tags.
     $tags = $this->commentparser->gettagOrders();
     if (count($tags) > 1) {
         $newlinespan = $comment->getNewlineAfter();
         if ($newlinespan !== 2) {
             $error = "There must be exactly one blank line before the tags in {$type} comments";
             if ($long !== '') {
                 $newlinecount += substr_count($long, $phpcsfile->eolChar) - $newlinespan + 1;
             }
             $phpcsfile->addwarning($error, $commentstart + $newlinecount);
             $short = rtrim($short, $phpcsfile->eolChar . ' ');
         }
     }
     // Check each tag.
     $this->processtags($commentstart, $commentend);
 }
 /**
  * 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)
 {
     $tokens = $phpcsfile->gettokens();
     $token = $tokens[$stackptr];
     // Skip broken function declarations.
     if (isset($token['scope_opener']) === false || isset($token['parenthesis_opener']) === false) {
         return;
     }
     $params = array();
     foreach ($phpcsfile->getmethodparameters($stackptr) as $param) {
         $params[$param['name']] = $stackptr;
     }
     $next = ++$token['scope_opener'];
     $end = --$token['scope_closer'];
     $emptybody = true;
     for (; $next <= $end; ++$next) {
         $token = $tokens[$next];
         $code = $token['code'];
         // Ingorable tokens.
         if (in_array($code, PHP_CodeSniffer_tokens::$emptyTokens) === true) {
             continue;
         } else {
             if ($code === T_THROW && $emptybody === true) {
                 // Throw statement and an empty body indicate an interface method.
                 return;
             } else {
                 if ($code === T_RETURN && $emptybody === true) {
                     // Return statement and an empty body indicate an interface method.
                     $tmp = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, $next + 1, null, true);
                     if ($tmp === false) {
                         return;
                     }
                     // There is a return.
                     if ($tokens[$tmp] === T_SEMICOLON) {
                         return;
                     }
                     $tmp = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, $tmp + 1, null, true);
                     // There is a return <token>.
                     if ($tmp !== false && $tokens[$tmp] === T_SEMICOLON) {
                         return;
                     }
                 }
             }
         }
         $emptybody = false;
         if ($code === T_VARIABLE && isset($params[$token['content']]) === true) {
             unset($params[$token['content']]);
         } else {
             if ($code === T_DOUBLE_QUOTED_STRING) {
                 // tokenize double quote string.
                 $strtokens = token_get_all(sprintf('<?php %s;?>', $token['content']));
                 foreach ($strtokens as $tok) {
                     if (is_array($tok) === false || $tok[0] !== T_VARIABLE) {
                         continue;
                     }
                     if (isset($params[$tok[1]]) === true) {
                         unset($params[$tok[1]]);
                     }
                 }
             }
         }
     }
     if ($emptybody === false && count($params) > 0) {
         foreach ($params as $paramname => $position) {
             $error = 'The method parameter ' . $paramname . ' is never used';
             $phpcsfile->addwarning($error, $position);
         }
     }
 }
Example #11
0
 /**
  * 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)
 {
     $tokens = $phpcsfile->gettokens();
     // If this is an inline condition (ie. there is no scope opener), then
     // return, as this is not a new scope.
     if (isset($tokens[$stackptr]['scope_opener']) === false) {
         return;
     }
     if ($tokens[$stackptr]['code'] === T_ELSE) {
         $next = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, $stackptr + 1, null, true);
         // We will handle the T_IF token in another call to process.
         if ($tokens[$next]['code'] === T_IF) {
             return;
         }
     }
     // Find the first token on this line.
     $firsttoken = $stackptr;
     for ($i = $stackptr; $i >= 0; $i--) {
         // Record the first code token on the line.
         if (in_array($tokens[$i]['code'], PHP_CodeSniffer_tokens::$emptyTokens) === false) {
             $firsttoken = $i;
         }
         // It's the start of the line, so we've found our first php token.
         if ($tokens[$i]['column'] === 1) {
             break;
         }
     }
     // Based on the conditions that surround this token, determine the
     // indent that we expect this current content to be.
     $expectedindent = $this->calculateexpectedindent($tokens, $firsttoken);
     if ($tokens[$firsttoken]['column'] !== $expectedindent) {
         $error = 'line indented incorrectly; expected ';
         $error .= $expectedindent - 1 . ' spaces, found ';
         $error .= $tokens[$firsttoken]['column'] - 1;
         $phpcsfile->adderror($error, $stackptr);
     }
     $scopeopener = $tokens[$stackptr]['scope_opener'];
     $scopecloser = $tokens[$stackptr]['scope_closer'];
     // Some scopes are expected not to have indents.
     if (in_array($tokens[$firsttoken]['code'], $this->nonindentingscopes) === false) {
         $indent = $expectedindent + $this->indent;
     } else {
         $indent = $expectedindent;
     }
     $newline = false;
     $commentopen = false;
     $inheredoc = false;
     // Only loop over the content beween the opening and closing brace, not
     // the braces themselves.
     for ($i = $scopeopener + 1; $i < $scopecloser; $i++) {
         // If this token is another scope, skip it as it will be handled by
         // another call to this sniff.
         if (in_array($tokens[$i]['code'], PHP_CodeSniffer_tokens::$scopeOpeners) === true) {
             if (isset($tokens[$i]['scope_opener']) === true) {
                 $i = $tokens[$i]['scope_closer'];
             } else {
                 // If this token does not have a scope_opener indice, then
                 // it's probably an inline scope, so let's skip to the next
                 // semicolon. Inline scopes include inline if's, abstract methods etc.
                 $nexttoken = $phpcsfile->findnext(T_SEMICOLON, $i, $scopecloser);
                 if ($nexttoken !== false) {
                     $i = $nexttoken;
                 }
             }
             continue;
         }
         // If this is a HEREDOC then we need to ignore it as the whitespace
         // before the contents within the HEREDOC are considered part of the content.
         if ($tokens[$i]['code'] === T_START_HEREDOC) {
             $inheredoc = true;
             continue;
         } else {
             if ($inheredoc === true) {
                 if ($tokens[$i]['code'] === T_END_HEREDOC) {
                     $inheredoc = false;
                 }
                 continue;
             }
         }
         if ($tokens[$i]['column'] === 1) {
             // We started a newline.
             $newline = true;
         }
         if ($newline === true && $tokens[$i]['code'] !== T_WHITESPACE) {
             // If we started a newline and we find a token that is not
             // whitespace, then this must be the first token on the line that
             // must be indented.
             $newline = false;
             $firsttoken = $i;
             $column = $tokens[$firsttoken]['column'];
             // Special case for non-PHP code.
             if ($tokens[$firsttoken]['code'] === T_INLINE_HTML) {
                 $trimmedcontentlength = strlen(ltrim($tokens[$firsttoken]['content']));
                 if ($trimmedcontentlength === 0) {
                     continue;
                 }
                 $contentlength = strlen($tokens[$firsttoken]['content']);
                 $column = $contentlength - $trimmedcontentlength + 1;
             }
             // Check to see if this constant string spans multiple lines.
             // If so, then make sure that the strings on lines other than the
             // first line are indented appropriately, based on their whitespace.
             if (in_array($tokens[$firsttoken]['code'], PHP_CodeSniffer_tokens::$stringTokens) === true) {
                 if (in_array($tokens[$firsttoken - 1]['code'], PHP_CodeSniffer_tokens::$stringTokens) === true) {
                     // If we find a string that directly follows another string
                     // then its just a string that spans multiple lines, so we
                     // don't need to check for indenting.
                     continue;
                 }
             }
             // This is a special condition for T_DOC_COMMENT and C-style
             // comments, which contain whitespace between each line.
             if (in_array($tokens[$firsttoken]['code'], array(T_COMMENT, T_DOC_COMMENT)) === true) {
                 $content = trim($tokens[$firsttoken]['content']);
                 if (preg_match('|^/\\*|', $content) !== 0) {
                     // Check to see if the end of the comment is on the same line
                     // as the start of the comment. If it is, then we don't
                     // have to worry about opening a comment.
                     if (preg_match('|\\*/$|', $content) === 0) {
                         // We don't have to calculate the column for the start
                         // of the comment as there is a whitespace token before it.
                         $commentopen = true;
                     }
                 } else {
                     if ($commentopen === true) {
                         if ($content === '') {
                             // We are in a comment, but this line has nothing on it
                             // so let's skip it.
                             continue;
                         }
                         $contentlength = strlen($tokens[$firsttoken]['content']);
                         $trimmedcontentlength = strlen(ltrim($tokens[$firsttoken]['content']));
                         $column = $contentlength - $trimmedcontentlength + 1;
                         if (preg_match('|\\*/$|', $content) !== 0) {
                             $commentopen = false;
                         }
                     }
                 }
             }
             // The token at the start of the line, needs to have its' column
             // greater than the relative indent we set above. If it is less,
             // an error should be shown.
             if ($column !== $indent) {
                 if ($this->exact === true || $column < $indent) {
                     $error = 'line indented incorrectly; expected ';
                     if ($this->exact === false) {
                         $error .= 'at least ';
                     }
                     $error .= $indent - 1 . ' spaces, found ';
                     $error .= $column - 1;
                     $phpcsfile->adderror($error, $firsttoken);
                 }
             }
         }
     }
 }
 /**
  * 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)
 {
     $tokens = $phpcsfile->gettokens();
     // Skip tokens that are the names of functions or classes
     // within their definitions. For example:
     // function myFunction...
     // "myFunction" is T_STRING but we should skip because it is not a
     // function or method *call*.
     $functionname = $stackptr;
     $functionkeyword = $phpcsfile->findprevious(PHP_CodeSniffer_tokens::$emptyTokens, $stackptr - 1, null, true);
     if ($tokens[$functionkeyword]['code'] === T_FUNCTION || $tokens[$functionkeyword]['code'] === T_CLASS) {
         return;
     }
     // If the next non-whitespace token after the function or method call
     // is not an opening parenthesis then it cant really be a *call*.
     $openbracket = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, $functionname + 1, null, true);
     if ($tokens[$openbracket]['code'] !== T_OPEN_PARENTHESIS) {
         return;
     }
     $closebracket = $tokens[$openbracket]['parenthesis_closer'];
     $nextseparator = $openbracket;
     while (($nextseparator = $phpcsfile->findnext(array(T_COMMA, T_VARIABLE), $nextseparator + 1, $closebracket)) !== false) {
         // Make sure the comma or variable belongs directly to this function call,
         // and is not inside a nested function call or array.
         $brackets = $tokens[$nextseparator]['nested_parenthesis'];
         $lastbracket = array_pop($brackets);
         if ($lastbracket !== $closebracket) {
             continue;
         }
         if ($tokens[$nextseparator]['code'] === T_COMMA) {
             if ($tokens[$nextseparator - 1]['code'] === T_WHITESPACE) {
                 $error = 'Space found before comma in function call';
                 $phpcsfile->addwarning($error, $stackptr);
             }
             if ($tokens[$nextseparator + 1]['code'] !== T_WHITESPACE) {
                 $error = 'No space found after comma in function call';
                 $phpcsfile->addwarning($error, $stackptr);
             } else {
                 // If there is a newline in the space, then the must be formatting
                 // each argument on a newline, which is valid, so ignore it.
                 if (strpos($tokens[$nextseparator + 1]['content'], $phpcsfile->eolChar) === false) {
                     $space = strlen($tokens[$nextseparator + 1]['content']);
                     if ($space > 1) {
                         $error = 'Expected 1 space after comma in function call; ';
                         $error .= $space . ' found';
                         $phpcsfile->addwarning($error, $stackptr);
                     }
                 }
             }
         } else {
             // token is a variable.
             $nexttoken = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, $nextseparator + 1, $closebracket, true);
             if ($nexttoken !== false) {
                 if ($tokens[$nexttoken]['code'] === T_EQUAL) {
                     if ($tokens[$nexttoken - 1]['code'] !== T_WHITESPACE) {
                         $error = 'Expected 1 space before = sign of default value';
                         $phpcsfile->addwarning($error, $stackptr);
                     }
                     if ($tokens[$nexttoken + 1]['code'] !== T_WHITESPACE) {
                         $error = 'Expected 1 space after = sign of default value';
                         $phpcsfile->addwarning($error, $stackptr);
                     }
                 }
             }
         }
     }
 }
 /**
  * Returns true if the passed argument has a default value.
  *
  * @param PHP_CodeSniffer_File $phpcsfile The file being scanned.
  * @param int                  $argptr    The position of the argument
  *                                        in the stack.
  *
  * @return bool
  */
 private static function _arghasdefault(PHP_CodeSniffer_File $phpcsfile, $argptr)
 {
     $tokens = $phpcsfile->gettokens();
     $nexttoken = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, $argptr + 1, null, true);
     if ($tokens[$nexttoken]['code'] !== T_EQUAL) {
         return false;
     }
     return true;
 }
 /**
  * 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)
 {
     $tokens = $phpcsfile->gettokens();
     $token = $tokens[$stackptr];
     // Skip invalid statement.
     if (isset($token['parenthesis_opener']) === false) {
         return;
     }
     $next = ++$token['parenthesis_opener'];
     $end = --$token['parenthesis_closer'];
     $position = 0;
     for (; $next <= $end; ++$next) {
         $code = $tokens[$next]['code'];
         if ($code === T_SEMICOLON) {
             ++$position;
         }
         if ($position < 1) {
             continue;
         } else {
             if ($position > 1) {
                 break;
             } else {
                 if ($code !== T_VARIABLE && $code !== T_STRING) {
                     continue;
                 }
             }
         }
         // Find next non empty token, if it is a open curly brace we have a
         // function call.
         $index = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, $next + 1, null, true);
         if ($tokens[$index]['code'] === T_OPEN_PARENTHESIS) {
             $error = 'Avoid function calls in a FOR loop test part';
             $phpcsfile->addwarning($error, $stackptr);
             break;
         }
     }
 }
 /**
  * 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)
 {
     $tokens = $phpcsfile->gettokens();
     $constname = $tokens[$stackptr]['content'];
     // If this token is in a heredoc, ignore it.
     if ($phpcsfile->hasCondition($stackptr, T_START_HEREDOC) === true) {
         return;
     }
     // If the next non-whitespace token after this token
     // is not an opening parenthesis then it is not a function call.
     $openbracket = $phpcsfile->findnext(array(T_WHITESPACE), $stackptr + 1, null, true);
     if ($tokens[$openbracket]['code'] !== T_OPEN_PARENTHESIS) {
         $functionkeyword = $phpcsfile->findprevious(array(T_WHITESPACE, T_COMMA, T_COMMENT, T_STRING), $stackptr - 1, null, true);
         $declarations = array(T_FUNCTION, T_CLASS, T_INTERFACE, T_IMPLEMENTS, T_EXTENDS, T_INSTANCEOF, T_NEW);
         if (in_array($tokens[$functionkeyword]['code'], $declarations) === true) {
             // This is just a declaration; no constants here.
             return;
         }
         if ($tokens[$functionkeyword]['code'] === T_CONST) {
             // This is a class constant.
             if (strtoupper($constname) !== $constname) {
                 $error = 'Class constants must be uppercase; expected ' . strtoupper($constname) . " but found {$constname}";
                 $phpcsfile->adderror($error, $stackptr);
             }
             return;
         }
         // Is this a class name?
         $nextptr = $phpcsfile->findnext(array(T_WHITESPACE), $stackptr + 1, null, true);
         if ($tokens[$nextptr]['code'] === T_DOUBLE_COLON) {
             return;
         }
         // Is this a type hint?
         if ($tokens[$nextptr]['code'] === T_VARIABLE) {
             return;
         } else {
             if ($phpcsfile->isReference($nextptr) === true) {
                 return;
             }
         }
         // Is this a member var name?
         $prevptr = $phpcsfile->findprevious(array(T_WHITESPACE), $stackptr - 1, null, true);
         if ($tokens[$prevptr]['code'] === T_OBJECT_OPERATOR) {
             return;
         }
         // Is this an instance of declare()
         $prevptr = $phpcsfile->findprevious(array(T_WHITESPACE, T_OPEN_PARENTHESIS), $stackptr - 1, null, true);
         if ($tokens[$prevptr]['code'] === T_DECLARE) {
             return;
         }
         // This is a real constant.
         if (strtoupper($constname) !== $constname) {
             $error = 'Constants must be uppercase; expected ' . strtoupper($constname) . " but found {$constname}";
             $phpcsfile->adderror($error, $stackptr);
         }
     } else {
         if (strtolower($constname) === 'define' || strtolower($constname) === 'constant') {
             /*
                 This may be a "define" or "constant" function call.
             */
             // The next non-whitespace token must be the constant name.
             $constptr = $phpcsfile->findnext(array(T_WHITESPACE), $openbracket + 1, null, true);
             if ($tokens[$constptr]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
                 return;
             }
             $constname = $tokens[$constptr]['content'];
             if (strtoupper($constname) !== $constname) {
                 $error = 'Constants must be uppercase; expected ' . strtoupper($constname) . " but found {$constname}";
                 $phpcsfile->adderror($error, $stackptr);
             }
         }
     }
 }