PDepend\TextUI\Command::run PHP Method

run() public method

Performs the main cli process and returns the exit code.
public run ( ) : integer
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['--quiet'])) {
            $runSilent = true;
            unset($options['--quiet']);
        } else {
            $runSilent = false;
            $this->runner->addProcessListener(new \PDepend\TextUI\ResultPrinter());
        }
        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
            if ($runSilent === false) {
                $this->printVersion();
                $this->printWorkarounds();
            }
            $startTime = time();
            $result = $this->runner->run();
            if ($this->runner->hasParseErrors() === true) {
                $errors = $this->runner->getParseErrors();
                printf('%sThe following error%s occurred:%s', PHP_EOL, count($errors) > 1 ? 's' : '', PHP_EOL);
                foreach ($errors as $error) {
                    echo $error, PHP_EOL;
                }
                echo PHP_EOL;
            }
            if ($runSilent === false) {
                $this->printStatistics($startTime);
            }
            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();
        }
    }

Usage Example

Beispiel #1
0
 /**
  * Main method that starts the command line runner.
  *
  * @return integer The exit code.
  */
 public static function main()
 {
     $command = new Command();
     return $command->run();
 }
All Usage Examples Of PDepend\TextUI\Command::run