Example #1
0
 /**
  * Creates a {@link PHP_Depend_Code_ASTNode} instance.
  *
  * @param string $className Local name of the ast node class.
  * @param string $image     Optional image for the created ast node.
  *
  * @return PHP_Depend_Code_ASTNode
  * @since 0.9.12
  */
 private function _buildASTNodeInstance($className, $image = null)
 {
     $fileName = "PHP/Depend/Code/{$className}.php";
     $className = "PHP_Depend_Code_{$className}";
     include_once $fileName;
     PHP_Depend_Util_Log::debug("Creating: {$className}({$image})");
     return new $className($image);
 }
Example #2
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');
         }
     }
 }
Example #3
0
 /**
  * Parses the cli arguments.
  *
  * @return boolean
  */
 protected function handleArguments()
 {
     if (!isset($_SERVER['argv'])) {
         if (false === (bool) ini_get('register_argc_argv')) {
             // @codeCoverageIgnoreStart
             echo 'Please enable register_argc_argv in your php.ini.';
         } else {
             // @codeCoverageIgnoreEnd
             echo 'Unknown error, no $argv array available.';
         }
         echo PHP_EOL, PHP_EOL;
         return false;
     }
     // Get cli arguments
     $argv = $_SERVER['argv'];
     // Remove the pdepend command line file
     array_shift($argv);
     if (count($argv) === 0) {
         return false;
     }
     // Last argument must be a list of source directories
     if (strpos(end($argv), '--') !== 0) {
         $this->_runner->setSourceArguments(explode(',', array_pop($argv)));
     }
     for ($i = 0, $c = count($argv); $i < $c; ++$i) {
         // Is it an ini_set option?
         if ($argv[$i] === '-d' && isset($argv[$i + 1])) {
             if (strpos($argv[++$i], '=') === false) {
                 ini_set($argv[$i], 'on');
             } else {
                 // Split key=value
                 list($key, $value) = explode('=', $argv[$i]);
                 // set ini option
                 ini_set($key, $value);
             }
         } else {
             if (strpos($argv[$i], '=') === false) {
                 $this->_options[$argv[$i]] = true;
             } else {
                 // Split key=value
                 list($key, $value) = explode('=', $argv[$i]);
                 // Set option
                 $this->_options[$key] = $value;
             }
         }
     }
     // Check for suffix option
     if (isset($this->_options['--suffix'])) {
         // Get file extensions
         $extensions = explode(',', $this->_options['--suffix']);
         // Set allowed file extensions
         $this->_runner->setFileExtensions($extensions);
         // Remove from options array
         unset($this->_options['--suffix']);
     }
     // Check for ignore option
     if (isset($this->_options['--ignore'])) {
         // Get exclude directories
         $directories = explode(',', $this->_options['--ignore']);
         // Set exclude directories
         $this->_runner->setExcludeDirectories($directories);
         // Remove from options array
         unset($this->_options['--ignore']);
     }
     // Check for exclude package option
     if (isset($this->_options['--exclude'])) {
         // Get exclude directories
         $packages = explode(',', $this->_options['--exclude']);
         // Set exclude packages
         $this->_runner->setExcludePackages($packages);
         // Remove from options array
         unset($this->_options['--exclude']);
     }
     // Check for the bad documentation option
     if (isset($this->_options['--bad-documentation'])) {
         echo "Option --bad-documentation is ambiguous.", PHP_EOL;
         // Remove from options array
         unset($this->_options['--bad-documentation']);
     }
     // Check for configuration option
     if (isset($this->_options['--configuration'])) {
         // Get config file
         $configFile = $this->_options['--configuration'];
         // Remove option from array
         unset($this->_options['--configuration']);
         // First check config file
         if (file_exists($configFile) === false) {
             // Print error message
             echo 'The configuration file "', $configFile, '" doesn\'t exist.', PHP_EOL, PHP_EOL;
             // Return error
             return false;
         }
         // Load configuration file
         $config = new PHP_Depend_Util_Configuration($configFile, null, true);
         // Store in config registry
         PHP_Depend_Util_ConfigurationInstance::set($config);
     }
     if (isset($this->_options['--debug'])) {
         // Remove option from array
         unset($this->_options['--debug']);
         // Enable debug logging
         PHP_Depend_Util_Log::setSeverity(PHP_Depend_Util_Log::DEBUG);
     }
     return true;
 }
Example #4
0
 /**
  * Sets the log severity levels, this can be an OR combined list of valid
  * severities.
  *
  * @param integer $severity The log severity levels.
  *
  * @return void
  */
 public static function setSeverity($severity)
 {
     self::$_debug = (self::DEBUG & $severity) === $severity;
 }
Example #5
0
 /**
  * Parses the contents of the tokenizer and generates a node tree based on
  * the found tokens.
  *
  * @return void
  */
 public function parse()
 {
     $this->_sourceFile = $this->tokenizer->getSourceFile();
     $this->_sourceFile->setCache($this->cache)->setUUID($this->_uuidBuilder->forFile($this->_sourceFile));
     $hash = md5_file($this->_sourceFile->getFileName());
     if ($this->cache->restore($this->_sourceFile->getUUID(), $hash)) {
         return;
     }
     $this->cache->remove($this->_sourceFile->getUUID());
     $this->setUpEnvironment();
     $this->_tokenStack->push();
     PHP_Depend_Util_Log::debug('Processing file ' . $this->_sourceFile);
     $tokenType = $this->tokenizer->peek();
     while ($tokenType !== self::T_EOF) {
         switch ($tokenType) {
             case self::T_COMMENT:
                 $this->consumeToken(self::T_COMMENT);
                 break;
             case self::T_DOC_COMMENT:
                 $comment = $this->consumeToken(self::T_DOC_COMMENT)->image;
                 $this->_packageName = $this->_parsePackageAnnotation($comment);
                 $this->_docComment = $comment;
                 break;
             case self::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 self::T_NAMESPACE:
                 $this->_parseNamespaceDeclaration();
                 break;
             case self::T_NO_PHP:
             case self::T_OPEN_TAG:
             case self::T_OPEN_TAG_WITH_ECHO:
                 $this->consumeToken($tokenType);
                 $this->reset();
                 break;
             case self::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->_sourceFile->setTokens($this->_tokenStack->pop());
     $this->cache->store($this->_sourceFile->getUUID(), $this->_sourceFile, $hash);
     $this->tearDownEnvironment();
 }
 /**
  * @return PHP_Depend_TextUI_Runner
  */
 private function createLegacyRunner()
 {
     $runner = new PHP_Depend_TextUI_Runner();
     $runner->addProcessListener(new PHP_Depend_TextUI_ResultPrinter());
     if ($this->debug) {
         require_once 'PHP/Depend/Util/Log.php';
         // Enable debug logging
         PHP_Depend_Util_Log::setSeverity(PHP_Depend_Util_Log::DEBUG);
     }
     $configuration = $this->getConfiguration();
     if ($configuration === null) {
         $configurationFactory = new PHP_Depend_Util_Configuration_Factory();
         $configuration = $configurationFactory->createDefault();
     }
     PHP_Depend_Util_ConfigurationInstance::set($configuration);
     $runner->setConfiguration($configuration);
     return $runner;
 }
Example #7
0
 /**
  * Parses the contents of the tokenizer and generates a node tree based on
  * the found tokens.
  *
  * @return void
  */
 public function parse()
 {
     $this->setUpEnvironment();
     // Get currently parsed source file
     $this->_sourceFile = $this->_tokenizer->getSourceFile();
     $this->_sourceFile->setUUID($this->_uuidBuilder->forFile($this->_sourceFile));
     // Debug currently parsed source file.
     PHP_Depend_Util_Log::debug('Processing file ' . $this->_sourceFile);
     $tokenType = $this->_tokenizer->peek();
     while ($tokenType !== self::T_EOF) {
         switch ($tokenType) {
             case self::T_COMMENT:
                 $this->_consumeToken(self::T_COMMENT);
                 break;
             case self::T_DOC_COMMENT:
                 $comment = $this->_consumeToken(self::T_DOC_COMMENT)->image;
                 $this->_packageName = $this->_parsePackageAnnotation($comment);
                 $this->_docComment = $comment;
                 break;
             case self::T_INTERFACE:
                 $package = $this->_builder->buildPackage($this->_getNamespaceOrPackageName());
                 $package->addType($this->_parseInterfaceDeclaration());
                 break;
             case self::T_CLASS:
             case self::T_FINAL:
             case self::T_ABSTRACT:
                 $package = $this->_builder->buildPackage($this->_getNamespaceOrPackageName());
                 $package->addType($this->_parseClassDeclaration());
                 break;
             case self::T_FUNCTION:
                 $this->_parseFunctionOrClosureDeclaration();
                 break;
             case self::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 self::T_NAMESPACE:
                 $this->_parseNamespaceDeclaration();
                 break;
             default:
                 // Consume whatever token
                 $this->_consumeToken($tokenType);
                 $this->reset();
                 break;
         }
         $tokenType = $this->_tokenizer->peek();
     }
     $this->tearDownEnvironment();
 }
Example #8
0
 /**
  * Parses the cli arguments.
  *
  * @return boolean
  */
 protected function handleArguments()
 {
     if (!isset($_SERVER['argv'])) {
         if (false === (bool) ini_get('register_argc_argv')) {
             // @codeCoverageIgnoreStart
             echo 'Please enable register_argc_argv in your php.ini.';
         } else {
             // @codeCoverageIgnoreEnd
             echo 'Unknown error, no $argv array available.';
         }
         echo PHP_EOL, PHP_EOL;
         return false;
     }
     $argv = $_SERVER['argv'];
     // Remove the pdepend command line file
     array_shift($argv);
     if (count($argv) === 0) {
         return false;
     }
     // Last argument must be a list of source directories
     if (strpos(end($argv), '--') !== 0) {
         $this->runner->setSourceArguments(explode(',', array_pop($argv)));
     }
     for ($i = 0, $c = count($argv); $i < $c; ++$i) {
         // Is it an ini_set option?
         if ($argv[$i] === '-d' && isset($argv[$i + 1])) {
             if (strpos($argv[++$i], '=') === false) {
                 ini_set($argv[$i], 'on');
             } else {
                 list($key, $value) = explode('=', $argv[$i]);
                 ini_set($key, $value);
             }
         } else {
             if (strpos($argv[$i], '=') === false) {
                 $this->options[$argv[$i]] = true;
             } else {
                 list($key, $value) = explode('=', $argv[$i]);
                 $this->options[$key] = $value;
             }
         }
     }
     // Check for suffix option
     if (isset($this->options['--suffix'])) {
         // Get file extensions
         $extensions = explode(',', $this->options['--suffix']);
         // Set allowed file extensions
         $this->runner->setFileExtensions($extensions);
         unset($this->options['--suffix']);
     }
     // Check for ignore option
     if (isset($this->options['--ignore'])) {
         // Get exclude directories
         $directories = explode(',', $this->options['--ignore']);
         // Set exclude directories
         $this->runner->setExcludeDirectories($directories);
         unset($this->options['--ignore']);
     }
     // Check for exclude package option
     if (isset($this->options['--exclude'])) {
         // Get exclude directories
         $packages = explode(',', $this->options['--exclude']);
         // Set exclude packages
         $this->runner->setExcludePackages($packages);
         unset($this->options['--exclude']);
     }
     // Check for the bad documentation option
     if (isset($this->options['--bad-documentation'])) {
         echo "Option --bad-documentation is ambiguous.", PHP_EOL;
         unset($this->options['--bad-documentation']);
     }
     $configurationFactory = new PHP_Depend_Util_Configuration_Factory();
     // Check for configuration option
     if (isset($this->options['--configuration'])) {
         // Get config file
         $configFile = $this->options['--configuration'];
         unset($this->options['--configuration']);
         $configuration = $configurationFactory->create($configFile);
     } else {
         $configuration = $configurationFactory->createDefault();
     }
     // Store in config registry
     PHP_Depend_Util_ConfigurationInstance::set($configuration);
     $this->runner->setConfiguration($configuration);
     if (isset($this->options['--debug'])) {
         unset($this->options['--debug']);
         PHP_Depend_Util_Log::setSeverity(PHP_Depend_Util_Log::DEBUG);
     }
     return true;
 }
Example #9
0
 /**
  * Sets the log severity levels, this can be an OR combined list of valid
  * severities.
  *
  * @param integer $severity The log severity levels.
  *
  * @return void
  */
 public static function setSeverity($severity)
 {
     self::$_severity = $severity;
 }