Пример #1
0
 /**
  * Parses the contents of the tokenizer and generates a node tree based on
  * the found tokens.
  *
  * @return void
  */
 public function parse()
 {
     $this->compilationUnit = $this->tokenizer->getSourceFile();
     $this->compilationUnit->setCache($this->cache)->setId($this->idBuilder->forFile($this->compilationUnit));
     $hash = md5_file($this->compilationUnit->getFileName());
     if ($this->cache->restore($this->compilationUnit->getId(), $hash)) {
         return;
     }
     $this->cache->remove($this->compilationUnit->getId());
     $this->setUpEnvironment();
     $this->tokenStack->push();
     Log::debug('Processing file ' . $this->compilationUnit);
     $tokenType = $this->tokenizer->peek();
     while ($tokenType !== Tokenizer::T_EOF) {
         switch ($tokenType) {
             case Tokens::T_COMMENT:
                 $this->consumeToken(Tokens::T_COMMENT);
                 break;
             case Tokens::T_DOC_COMMENT:
                 $comment = $this->consumeToken(Tokens::T_DOC_COMMENT)->image;
                 $this->packageName = $this->parsePackageAnnotation($comment);
                 $this->docComment = $comment;
                 break;
             case Tokens::T_USE:
                 // Parse a use statement. This method has no return value but it
                 // creates a new entry in the symbol map.
                 $this->parseUseDeclarations();
                 break;
             case Tokens::T_NAMESPACE:
                 $this->parseNamespaceDeclaration();
                 break;
             case Tokens::T_NO_PHP:
             case Tokens::T_OPEN_TAG:
             case Tokens::T_OPEN_TAG_WITH_ECHO:
                 $this->consumeToken($tokenType);
                 $this->reset();
                 break;
             case Tokens::T_CLOSE_TAG:
                 $this->parseNonePhpCode();
                 $this->reset();
                 break;
             default:
                 if (null === $this->parseOptionalStatement()) {
                     // Consume whatever token
                     $this->consumeToken($tokenType);
                 }
                 break;
         }
         $tokenType = $this->tokenizer->peek();
     }
     $this->compilationUnit->setTokens($this->tokenStack->pop());
     $this->cache->store($this->compilationUnit->getId(), $this->compilationUnit, $hash);
     $this->tearDownEnvironment();
 }
Пример #2
0
    /**
     * Performs the main cli process and returns the exit code.
     *
     * @return integer
     */
    public function run()
    {
        $this->application = new Application();

        try {
            if ($this->parseArguments() === false) {
                $this->printHelp();
                return self::CLI_ERROR;
            }
        } catch (\Exception $e) {
            echo $e->getMessage(), PHP_EOL, PHP_EOL;

            $this->printHelp();
            return self::CLI_ERROR;
        }

        if (isset($this->options['--help'])) {
            $this->printHelp();
            return Runner::SUCCESS_EXIT;
        }
        if (isset($this->options['--usage'])) {
            $this->printUsage();
            return Runner::SUCCESS_EXIT;
        }
        if (isset($this->options['--version'])) {
            $this->printVersion();
            return Runner::SUCCESS_EXIT;
        }

        $configurationFile = false;

        if (isset($this->options['--configuration'])) {
            $configurationFile = $this->options['--configuration'];

            if (false === file_exists($configurationFile)) {
                $configurationFile = getcwd() . '/' . $configurationFile;
            }
            if (false === file_exists($configurationFile)) {
                $configurationFile = $this->options['--configuration'];
            }

            unset($this->options['--configuration']);
        } elseif (file_exists(getcwd() . '/pdepend.xml')) {
            $configurationFile = getcwd() . '/pdepend.xml';
        } elseif (file_exists(getcwd() . '/pdepend.xml.dist')) {
            $configurationFile = getcwd() . '/pdepend.xml.dist';
        }

        if ($configurationFile) {
            try {
                $this->application->setConfigurationFile($configurationFile);
            } catch (\Exception $e) {
                echo $e->getMessage(), PHP_EOL, PHP_EOL;

                $this->printHelp();
                return self::CLI_ERROR;
            }
        }

        // Create a new text ui runner
        $this->runner = $this->application->getRunner();

        $this->assignArguments();

        // Get a copy of all options
        $options = $this->options;

        // Get an array with all available log options
        $logOptions = $this->application->getAvailableLoggerOptions();

        // Get an array with all available analyzer options
        $analyzerOptions = $this->application->getAvailableAnalyzerOptions();

        foreach ($options as $option => $value) {
            if (isset($logOptions[$option])) {
                // Reduce recieved option list
                unset($options[$option]);
                // Register logger
                $this->runner->addReportGenerator(substr($option, 2), $value);
            } elseif (isset($analyzerOptions[$option])) {
                // Reduce recieved option list
                unset($options[$option]);

                if (isset($analyzerOptions[$option]['value']) && is_bool($value)) {
                    echo 'Option ', $option, ' requires a value.', PHP_EOL;
                    return self::INPUT_ERROR;
                } elseif ($analyzerOptions[$option]['value'] === 'file'
                    && file_exists($value) === false
                ) {
                    echo 'Specified file ', $option, '=', $value,
                         ' not exists.', PHP_EOL;

                    return self::INPUT_ERROR;
                } elseif ($analyzerOptions[$option]['value'] === '*[,...]') {
                    $value = array_map('trim', explode(',', $value));
                }
                $this->runner->addOption(substr($option, 2), $value);
            }
        }

        if (isset($options['--without-annotations'])) {
            // Disable annotation parsing
            $this->runner->setWithoutAnnotations();
            // Remove option
            unset($options['--without-annotations']);
        }

        if (isset($options['--optimization'])) {
            // This option is deprecated.
            echo 'Option --optimization is ambiguous.', PHP_EOL;
            // Remove option
            unset($options['--optimization']);
        }

        if (isset($options['--notify-me'])) {
            $this->runner->addProcessListener(
                new \PDepend\DbusUI\ResultPrinter()
            );
            unset($options['--notify-me']);
        }

        if (count($options) > 0) {
            $this->printHelp();
            echo "Unknown option '", key($options), "' given.", PHP_EOL;
            return self::CLI_ERROR;
        }

        try {
            // Output current pdepend version and author
            $this->printVersion();
            $this->printWorkarounds();

            $startTime = time();

            $result = $this->runner->run();

            if ($this->runner->hasParseErrors() === true) {
                $errors = $this->runner->getParseErrors();

                printf(
                    '%sThe following error%s occured:%s',
                    PHP_EOL,
                    count($errors) > 1 ? 's' : '',
                    PHP_EOL
                );

                foreach ($errors as $error) {
                    echo $error, PHP_EOL;
                }
                echo PHP_EOL;
            }

            $duration = time() - $startTime;
            $hours = intval($duration / 3600);
            $minutes = intval(($duration - $hours * 3600) / 60);
            $seconds = $duration % 60;
            echo PHP_EOL, 'Time: ', sprintf('%d:%02d:%02d', $hours, $minutes, $seconds);
            if (function_exists('memory_get_peak_usage')) {
                $memory = (memory_get_peak_usage(true) / (1024 * 1024));
                printf('; Memory: %4.2fMb', $memory);
            }
            echo PHP_EOL;

            return $result;
        } catch (\RuntimeException $e) {

            echo PHP_EOL, PHP_EOL,
                 'Critical error: ', PHP_EOL,
                 '=============== ', PHP_EOL,
                  $e->getMessage(),  PHP_EOL;

            Log::debug($e->getTraceAsString());

            return $e->getCode();
        }
    }
Пример #3
0
 /**
  * Creates a {@link \PDepend\Source\AST\ASTNode} instance.
  *
  * @param string $className Local name of the ast node class.
  * @param string $image     Optional image for the created ast node.
  *
  * @return \PDepend\Source\AST\ASTNode
  * @since  0.9.12
  */
 private function buildAstNodeInstance($className, $image = null)
 {
     $className = "\\PDepend\\Source\\AST\\{$className}";
     Log::debug("Creating: {$className}({$image})");
     return new $className($image);
 }
 /**
  * Removes the given cache file.
  *
  * @param \SplFileInfo $file
  * @return void
  */
 private function garbageCollectFile(\SplFileInfo $file)
 {
     Log::debug("Removing file '{$file->getPathname()}' from cache.");
     @unlink($file->getPathname());
 }