Пример #1
0
 /**
  * Executes PHP_Depend_TextUI_Runner against PhingFile or a FileSet
  *
  * @return void
  * @throws BuildException
  */
 public function main()
 {
     $autoload = new PHP_Depend_Autoload();
     $autoload->register();
     if (!isset($this->_file) and count($this->_filesets) == 0) {
         throw new BuildException("Missing either a nested fileset or attribute 'file' set");
     }
     if (count($this->_loggers) == 0) {
         throw new BuildException("Missing nested 'logger' element");
     }
     $this->validateLoggers();
     $this->validateAnalyzers();
     $filesToParse = array();
     if ($this->_file instanceof PhingFile) {
         $filesToParse[] = $this->_file->__toString();
     } else {
         // append any files in filesets
         foreach ($this->_filesets as $fs) {
             $files = $fs->getDirectoryScanner($this->project)->getIncludedFiles();
             foreach ($files as $filename) {
                 $f = new PhingFile($fs->getDir($this->project), $filename);
                 $filesToParse[] = $f->getAbsolutePath();
             }
         }
     }
     $this->_runner = new PHP_Depend_TextUI_Runner();
     $this->_runner->addProcessListener(new PHP_Depend_TextUI_ResultPrinter());
     $configurationFactory = new PHP_Depend_Util_Configuration_Factory();
     $configuration = $configurationFactory->createDefault();
     $this->_runner->setConfiguration($configuration);
     $this->_runner->setSourceArguments($filesToParse);
     foreach ($this->_loggers as $logger) {
         // Register logger
         $this->_runner->addLogger($logger->getType(), $logger->getOutfile()->__toString());
     }
     foreach ($this->_analyzers as $analyzer) {
         // Register additional analyzer
         $this->_runner->addOption($analyzer->getType(), $analyzer->getValue());
     }
     // Disable annotation parsing
     if ($this->_withoutAnnotations) {
         $this->_runner->setWithoutAnnotations();
     }
     // Enable bad documentation support
     if ($this->_supportBadDocumentation) {
         $this->_runner->setSupportBadDocumentation();
     }
     // Check for suffix
     if (count($this->_allowedFileExtensions) > 0) {
         $this->_runner->setFileExtensions($this->_allowedFileExtensions);
     }
     // Check for ignore directories
     if (count($this->_excludeDirectories) > 0) {
         $this->_runner->setExcludeDirectories($this->_excludeDirectories);
     }
     // Check for exclude packages
     if (count($this->_excludePackages) > 0) {
         $this->_runner->setExcludePackages($this->_excludePackages);
     }
     // Check for configuration option
     if ($this->_configFile instanceof PhingFile) {
         if (file_exists($this->_configFile->__toString()) === false) {
             throw new BuildException('The configuration file "' . $this->_configFile->__toString() . '" doesn\'t exist.');
         }
         // Load configuration file
         $config = new PHP_Depend_Util_Configuration($this->_configFile->__toString(), null, true);
         // Store in config registry
         PHP_Depend_Util_ConfigurationInstance::set($config);
     }
     if ($this->_debug) {
         require_once 'PHP/Depend/Util/Log.php';
         // Enable debug logging
         PHP_Depend_Util_Log::setSeverity(PHP_Depend_Util_Log::DEBUG);
     }
     $this->_runner->run();
     if ($this->_runner->hasParseErrors() === true) {
         $this->log('Following errors occurred:');
         foreach ($this->_runner->getParseErrors() as $error) {
             $this->log($error);
         }
         if ($this->_haltonerror === true) {
             throw new BuildException('Errors occurred during parse process');
         }
     }
 }
Пример #2
0
 /**
  * Executes the runner class and returns an array with package statistics.
  *
  * @param array  PHP_Depend_TextUI_Runner $runner   The runner instance.
  * @param string                          $pathName The source path.
  *
  * @return array
  */
 private function _runRunnerAndReturnStatistics(PHP_Depend_TextUI_Runner $runner, $pathName)
 {
     $logFile = self::createRunResourceURI('pdepend.dummy');
     $runner->setSourceArguments(array($pathName));
     $runner->addLogger('dummy-logger', $logFile);
     ob_start();
     $runner->run();
     ob_end_clean();
     $data = unserialize(file_get_contents($logFile));
     $code = $data['code'];
     $actual = array();
     foreach ($code as $package) {
         $exceptions = 0;
         foreach ($package->getFunctions() as $function) {
             $exceptions += $function->getExceptionClasses()->count();
         }
         $actual[$package->getName()] = array('functions' => $package->getFunctions()->count(), 'classes' => $package->getClasses()->count(), 'interfaces' => $package->getInterfaces()->count(), 'exceptions' => $exceptions);
     }
     ksort($actual);
     return $actual;
 }
Пример #3
0
 /**
  * Performs the main cli process and returns the exit code.
  *
  * @return integer
  */
 public function run()
 {
     // Create a new text ui runner
     $this->_runner = new PHP_Depend_TextUI_Runner();
     $this->_runner->addProcessListener(new PHP_Depend_TextUI_ResultPrinter());
     if ($this->handleArguments() === false) {
         $this->printHelp();
         return self::CLI_ERROR;
     }
     if (isset($this->_options['--help'])) {
         $this->printHelp();
         return PHP_Depend_TextUI_Runner::SUCCESS_EXIT;
     }
     if (isset($this->_options['--usage'])) {
         $this->printUsage();
         return PHP_Depend_TextUI_Runner::SUCCESS_EXIT;
     }
     if (isset($this->_options['--version'])) {
         $this->printVersion();
         return PHP_Depend_TextUI_Runner::SUCCESS_EXIT;
     }
     // Get a copy of all options
     $options = $this->_options;
     // Get an array with all available log options
     $logOptions = $this->collectLogOptions();
     // Get an array with all available analyzer options
     $analyzerOptions = $this->collectAnalyzerOptions();
     foreach ($options as $option => $value) {
         if (isset($logOptions[$option])) {
             // Reduce recieved option list
             unset($options[$option]);
             // Register logger
             $this->_runner->addLogger(substr($option, 2), $value);
         } else {
             if (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;
                 } else {
                     if ($analyzerOptions[$option]['value'] === 'file' && file_exists($value) === false) {
                         echo 'Specifie file ', $option, '=', $value, ' not exists.', PHP_EOL;
                         return self::INPUT_ERROR;
                     } else {
                         if ($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'])) {
         // Check optimization strategy
         if (!isset($this->_optimizations[$options['--optimization']])) {
             echo 'Invalid optimization ', $options['--optimization'], ' given.', PHP_EOL;
             return self::INPUT_ERROR;
         }
         // Set optimization strategy
         $this->_runner->setOptimization($options['--optimization']);
         // Remove option
         unset($options['--optimization']);
     }
     if (isset($options['--notify-me'])) {
         // Import the class source
         include_once 'PHP/Depend/DbusUI/ResultPrinter.php';
         // Load the dbus result printer.
         $this->_runner->addProcessListener(new PHP_Depend_DbusUI_ResultPrinter());
         // Remove that option
         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();
         $startTime = time();
         $result = $this->_runner->run();
         if ($this->_runner->hasParseErrors() === true) {
             echo PHP_EOL, 'Following errors occured:', PHP_EOL;
             foreach ($this->_runner->getParseErrors() as $error) {
                 echo $error, PHP_EOL;
             }
             echo PHP_EOL;
         }
         echo PHP_EOL, 'Time: ', date('i:s', time() - $startTime);
         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) {
         // Print error message
         echo $e->getMessage(), PHP_EOL;
         // Return exit code
         return $e->getCode();
     }
 }
 /**
  * Tests that the {@link PHP_Depend_TextUI_Runner::hasErrors()} method will
  * return <b>true</b> when a parsing error occured.
  *
  * @return void
  * @covers PHP_Depend_TextUI_Runner
  */
 public function testRunnerReturnsTrueWhenAnErrorOccuredDuringTheParsingProcess()
 {
     $runner = new PHP_Depend_TextUI_Runner();
     $runner->setConfiguration($this->createConfigurationFixture());
     $runner->addLogger('dummy-logger', self::createRunResourceURI('pdepend.log'));
     $runner->setSourceArguments(array(self::createCodeResourceUriForTest()));
     $runner->run();
     self::assertTrue($runner->hasParseErrors());
 }
Пример #5
0
 /**
  * Performs the main cli process and returns the exit code.
  *
  * @return integer
  */
 public function run()
 {
     // Create a new text ui runner
     $this->runner = new PHP_Depend_TextUI_Runner();
     $this->runner->addProcessListener(new PHP_Depend_TextUI_ResultPrinter());
     try {
         if ($this->handleArguments() === 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 PHP_Depend_TextUI_Runner::SUCCESS_EXIT;
     }
     if (isset($this->options['--usage'])) {
         $this->printUsage();
         return PHP_Depend_TextUI_Runner::SUCCESS_EXIT;
     }
     if (isset($this->options['--version'])) {
         $this->printVersion();
         return PHP_Depend_TextUI_Runner::SUCCESS_EXIT;
     }
     // Get a copy of all options
     $options = $this->options;
     // Get an array with all available log options
     $logOptions = $this->collectLogOptions();
     // Get an array with all available analyzer options
     $analyzerOptions = $this->collectAnalyzerOptions();
     foreach ($options as $option => $value) {
         if (isset($logOptions[$option])) {
             // Reduce recieved option list
             unset($options[$option]);
             // Register logger
             $this->runner->addLogger(substr($option, 2), $value);
         } else {
             if (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;
                 } else {
                     if ($analyzerOptions[$option]['value'] === 'file' && file_exists($value) === false) {
                         echo 'Specified file ', $option, '=', $value, ' not exists.', PHP_EOL;
                         return self::INPUT_ERROR;
                     } else {
                         if ($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 PHP_Depend_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();
         $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;
         }
         echo PHP_EOL, 'Time: ', date('i:s', time() - $startTime);
         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;
         return $e->getCode();
     }
 }
Пример #6
0
 /**
  * testSupportBadDocumentation
  *
  * @return void
  * @covers PHP_Depend_TextUI_Runner
  * @group pdepend
  * @group pdepend::textui
  * @group unittest
  */
 public function testSupportBadDocumentation()
 {
     $fileName = self::createRunResourceURI('pdepend.dummy');
     $runner = new PHP_Depend_TextUI_Runner();
     $runner->setSourceArguments(array(dirname(__FILE__) . '/../_code/code-without-comments'));
     $runner->setSupportBadDocumentation();
     $runner->addLogger('dummy-logger', $fileName);
     ob_start();
     $runner->run();
     ob_end_clean();
     $data = unserialize(file_get_contents($fileName));
     $code = $data['code'];
     $this->assertEquals(2, $code->count());
     $code->rewind();
     $package = $code->current();
     $this->assertEquals(PHP_Depend_BuilderI::DEFAULT_PACKAGE, $package->getName());
     $this->assertEquals(7, $package->getClasses()->count());
     $this->assertEquals(3, $package->getInterfaces()->count());
 }