PHP_CodeSniffer_File::start PHP Метод

start() публичный Метод

Starts the stack traversal and tells listeners when tokens are found.
public start ( string $contents = null ) : void
$contents string The contents to parse. If NULL, the content is taken from the file system.
Результат void
    public function start($contents = null)
    {
        $this->_errors = array();
        $this->_warnings = array();
        $this->_errorCount = 0;
        $this->_warningCount = 0;
        $this->_fixableCount = 0;
        // Reset the ignored lines because lines numbers may have changed
        // if we are fixing this file.
        self::$_ignoredLines = array();
        try {
            $this->eolChar = self::detectLineEndings($this->_file, $contents);
        } catch (PHP_CodeSniffer_Exception $e) {
            $this->addWarning($e->getMessage(), null, 'Internal.DetectLineEndings');
            return;
        }
        // If this is standard input, see if a filename was passed in as well.
        // This is done by including: phpcs_input_file: [file path]
        // as the first line of content.
        if ($this->_file === 'STDIN') {
            $cliValues = $this->phpcs->cli->getCommandLineValues();
            if ($cliValues['stdinPath'] !== '') {
                $this->_file = $cliValues['stdinPath'];
            } else {
                if ($contents !== null && substr($contents, 0, 17) === 'phpcs_input_file:') {
                    $eolPos = strpos($contents, $this->eolChar);
                    $filename = trim(substr($contents, 17, $eolPos - 17));
                    $contents = substr($contents, $eolPos + strlen($this->eolChar));
                    $this->_file = $filename;
                }
            }
        }
        $this->_parse($contents);
        $this->fixer->startFile($this);
        if (PHP_CODESNIFFER_VERBOSITY > 2) {
            echo "\t*** START TOKEN PROCESSING ***" . PHP_EOL;
        }
        $foundCode = false;
        $listeners = $this->phpcs->getSniffs();
        $listenerIgnoreTo = array();
        $inTests = defined('PHP_CODESNIFFER_IN_TESTS');
        // Foreach of the listeners that have registered to listen for this
        // token, get them to process it.
        foreach ($this->_tokens as $stackPtr => $token) {
            // Check for ignored lines.
            if ($token['code'] === T_COMMENT || $token['code'] === T_DOC_COMMENT_TAG || $inTests === true && $token['code'] === T_INLINE_HTML) {
                if (strpos($token['content'], '@codingStandards') !== false) {
                    if (strpos($token['content'], '@codingStandardsIgnoreFile') !== false) {
                        // Ignoring the whole file, just a little late.
                        $this->_errors = array();
                        $this->_warnings = array();
                        $this->_errorCount = 0;
                        $this->_warningCount = 0;
                        $this->_fixableCount = 0;
                        return;
                    } else {
                        if (strpos($token['content'], '@codingStandardsChangeSetting') !== false) {
                            $start = strpos($token['content'], '@codingStandardsChangeSetting');
                            $comment = substr($token['content'], $start + 30);
                            $parts = explode(' ', $comment);
                            if (count($parts) >= 3 && isset($this->phpcs->sniffCodes[$parts[0]]) === true) {
                                $listenerCode = array_shift($parts);
                                $propertyCode = array_shift($parts);
                                $propertyValue = rtrim(implode(' ', $parts), " */\r\n");
                                $listenerClass = $this->phpcs->sniffCodes[$listenerCode];
                                $this->phpcs->setSniffProperty($listenerClass, $propertyCode, $propertyValue);
                            }
                        }
                    }
                    //end if
                }
                //end if
            }
            //end if
            if (PHP_CODESNIFFER_VERBOSITY > 2) {
                $type = $token['type'];
                $content = PHP_CodeSniffer::prepareForOutput($token['content']);
                echo "\t\tProcess token {$stackPtr}: {$type} => {$content}" . PHP_EOL;
            }
            if ($token['code'] !== T_INLINE_HTML) {
                $foundCode = true;
            }
            if (isset($this->_listeners[$token['code']]) === false) {
                continue;
            }
            foreach ($this->_listeners[$token['code']] as $listenerData) {
                if (isset($this->_ignoredListeners[$listenerData['class']]) === true || isset($listenerIgnoreTo[$listenerData['class']]) === true && $listenerIgnoreTo[$listenerData['class']] > $stackPtr) {
                    // This sniff is ignoring past this token, or the whole file.
                    continue;
                }
                // Make sure this sniff supports the tokenizer
                // we are currently using.
                $class = $listenerData['class'];
                if (isset($listenerData['tokenizers'][$this->tokenizerType]) === false) {
                    continue;
                }
                // If the file path matches one of our ignore patterns, skip it.
                // While there is support for a type of each pattern
                // (absolute or relative) we don't actually support it here.
                foreach ($listenerData['ignore'] as $pattern) {
                    // We assume a / directory separator, as do the exclude rules
                    // most developers write, so we need a special case for any system
                    // that is different.
                    if (DIRECTORY_SEPARATOR === '\\') {
                        $pattern = str_replace('/', '\\\\', $pattern);
                    }
                    $pattern = '`' . $pattern . '`i';
                    if (preg_match($pattern, $this->_file) === 1) {
                        $this->_ignoredListeners[$class] = true;
                        continue 2;
                    }
                }
                $this->_activeListener = $class;
                if (PHP_CODESNIFFER_VERBOSITY > 2) {
                    $startTime = microtime(true);
                    echo "\t\t\tProcessing " . $this->_activeListener . '... ';
                }
                $ignoreTo = $listeners[$class]->process($this, $stackPtr);
                if ($ignoreTo !== null) {
                    $listenerIgnoreTo[$this->_activeListener] = $ignoreTo;
                }
                if (PHP_CODESNIFFER_VERBOSITY > 2) {
                    $timeTaken = microtime(true) - $startTime;
                    if (isset($this->_listenerTimes[$this->_activeListener]) === false) {
                        $this->_listenerTimes[$this->_activeListener] = 0;
                    }
                    $this->_listenerTimes[$this->_activeListener] += $timeTaken;
                    $timeTaken = round($timeTaken, 4);
                    echo "DONE in {$timeTaken} seconds" . PHP_EOL;
                }
                $this->_activeListener = '';
            }
            //end foreach
        }
        //end foreach
        if ($this->_recordErrors === false) {
            $this->_errors = array();
            $this->_warnings = array();
        }
        // If short open tags are off but the file being checked uses
        // short open tags, the whole content will be inline HTML
        // and nothing will be checked. So try and handle this case.
        if ($foundCode === false && $this->tokenizerType === 'PHP') {
            $shortTags = (bool) ini_get('short_open_tag');
            if ($shortTags === false) {
                $error = 'No PHP code was found in this file and short open tags are not allowed by this install of PHP. This file may be using short open tags but PHP does not allow them.';
                $this->addWarning($error, null, 'Internal.NoCodeFound');
            }
        }
        if (PHP_CODESNIFFER_VERBOSITY > 2) {
            echo "\t*** END TOKEN PROCESSING ***" . PHP_EOL;
            echo "\t*** START SNIFF PROCESSING REPORT ***" . PHP_EOL;
            asort($this->_listenerTimes, SORT_NUMERIC);
            $this->_listenerTimes = array_reverse($this->_listenerTimes, true);
            foreach ($this->_listenerTimes as $listener => $timeTaken) {
                echo "\t{$listener}: " . round($timeTaken, 4) . ' secs' . PHP_EOL;
            }
            echo "\t*** END SNIFF PROCESSING REPORT ***" . PHP_EOL;
        }
    }

Usage Example

 /**
  * Initialize & tokenize PHP_CodeSniffer_File with code from this file.
  *
  * Methods used for these tests can be found at the bottom of
  * this file.
  *
  * @return void
  */
 public function setUp()
 {
     $phpcs = new PHP_CodeSniffer();
     $this->_phpcsFile = new PHP_CodeSniffer_File(__FILE__, array(), array(), array(), $phpcs);
     $contents = file_get_contents(__FILE__);
     $this->_phpcsFile->start($contents);
 }
All Usage Examples Of PHP_CodeSniffer_File::start