setCommandLineValues() public method

Set the command line values.
public setCommandLineValues ( array $args ) : void
$args array An array of command line arguments to process.
return void
Ejemplo n.º 1
0
 public function setCommandLineValues($args)
 {
     parent::setCommandLineValues($args);
     if (!empty($this->values['files'])) {
         return;
     }
     $files = $this->getConfig('files', []);
     foreach (call_user_func($this->finderByConfig, $files) as $file) {
         $this->processUnknownArgument($file->getPathname(), -1);
     }
 }
Ejemplo n.º 2
0
 /**
  * Execute the command.
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  *
  * @return int
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $this->setupFormatters($output->getFormatter());
     $finder = new Finder();
     $phpcs = new CodeSniffer(0);
     $phpcsCli = new CLI();
     $phpcsCli->errorSeverity = PHPCS_DEFAULT_ERROR_SEV;
     $phpcsCli->warningSeverity = PHPCS_DEFAULT_WARN_SEV;
     $phpcsCli->dieOnUnknownArg = false;
     $phpcsCli->setCommandLineValues(['--colors', '-p', '--report=full']);
     $phpcs->setCli($phpcsCli);
     $existing = [];
     foreach (RootDirectories::getEnforceable() as $directory) {
         if (file_exists($directory) && is_dir($directory)) {
             $existing[] = $directory;
         }
     }
     $files = $finder->files()->in($existing)->notName('*Sniff.php')->ignoreUnreadableDirs()->ignoreDotFiles(true)->ignoreVCS(true)->name('*.php');
     $phpcs->reporting->startTiming();
     $phpcs->initStandard(Anchor::getDirectory());
     $files = array_keys(iterator_to_array($files->getIterator()));
     $processed = [];
     $withErrors = [];
     $withWarnings = [];
     foreach ($files as $file) {
         $done = $phpcs->processFile($file);
         if ($done->getErrorCount() > 0) {
             $output->write('E');
             $withErrors[] = $done;
             if ($done->getWarningCount() > 0) {
                 $withWarnings[] = $done;
             }
         } elseif ($done->getWarningCount() > 0) {
             $output->write('W');
             $withWarnings[] = $done;
         } else {
             $output->write('.');
         }
         $processed[] = $done;
     }
     $this->renderSummary($withErrors, $withWarnings, $output);
 }
Ejemplo n.º 3
0
 /**
  * Processes a single ruleset and returns a list of the sniffs it represents.
  *
  * Rules founds within the ruleset are processed immediately, but sniff classes
  * are not registered by this method.
  *
  * @param string $rulesetPath The path to a ruleset XML file.
  * @param int    $depth       How many nested processing steps we are in. This
  *                            is only used for debug output.
  *
  * @return array
  * @throws PHP_CodeSniffer_Exception If the ruleset path is invalid.
  */
 public function processRuleset($rulesetPath, $depth = 0)
 {
     $rulesetPath = self::realpath($rulesetPath);
     if (PHP_CODESNIFFER_VERBOSITY > 1) {
         echo str_repeat("\t", $depth);
         echo "Processing ruleset {$rulesetPath}" . PHP_EOL;
     }
     $ruleset = simplexml_load_string(file_get_contents($rulesetPath));
     if ($ruleset === false) {
         throw new PHP_CodeSniffer_Exception("Ruleset {$rulesetPath} is not valid");
     }
     $ownSniffs = array();
     $includedSniffs = array();
     $excludedSniffs = array();
     $cliValues = $this->cli->getCommandLineValues();
     $rulesetDir = dirname($rulesetPath);
     $rulesetName = basename($rulesetPath);
     self::$rulesetDirs[] = $rulesetDir;
     if (is_dir($rulesetDir . DIRECTORY_SEPARATOR . 'Sniffs') === true) {
         if (PHP_CODESNIFFER_VERBOSITY > 1) {
             echo str_repeat("\t", $depth);
             echo "\tAdding sniff files from \"/.../" . basename($rulesetDir) . "/Sniffs/\" directory" . PHP_EOL;
         }
         $ownSniffs = $this->_expandSniffDirectory($rulesetDir . DIRECTORY_SEPARATOR . 'Sniffs', $depth);
     }
     foreach ($ruleset->rule as $rule) {
         if (isset($rule['ref']) === false || $this->_shouldProcessElement($rule) === false) {
             continue;
         }
         if (PHP_CODESNIFFER_VERBOSITY > 1) {
             echo str_repeat("\t", $depth);
             echo "\tProcessing rule \"" . $rule['ref'] . '"' . PHP_EOL;
         }
         $includedSniffs = array_merge($includedSniffs, $this->_expandRulesetReference($rule['ref'], $rulesetDir, $depth));
         if (isset($rule->exclude) === true) {
             foreach ($rule->exclude as $exclude) {
                 if ($this->_shouldProcessElement($exclude) === false) {
                     continue;
                 }
                 if (PHP_CODESNIFFER_VERBOSITY > 1) {
                     echo str_repeat("\t", $depth);
                     echo "\t\tExcluding rule \"" . $exclude['name'] . '"' . PHP_EOL;
                 }
                 // Check if a single code is being excluded, which is a shortcut
                 // for setting the severity of the message to 0.
                 $parts = explode('.', $exclude['name']);
                 if (count($parts) === 4) {
                     $this->ruleset[(string) $exclude['name']]['severity'] = 0;
                     if (PHP_CODESNIFFER_VERBOSITY > 1) {
                         echo str_repeat("\t", $depth);
                         echo "\t\t=> severity set to 0" . PHP_EOL;
                     }
                 } else {
                     $excludedSniffs = array_merge($excludedSniffs, $this->_expandRulesetReference($exclude['name'], $rulesetDir, $depth + 1));
                 }
             }
             //end foreach
         }
         //end if
         $this->_processRule($rule, $depth);
     }
     //end foreach
     // Process custom command line arguments.
     $cliArgs = array();
     foreach ($ruleset->{'arg'} as $arg) {
         if ($this->_shouldProcessElement($arg) === false) {
             continue;
         }
         if (isset($arg['name']) === true) {
             $argString = '--' . (string) $arg['name'];
             if (isset($arg['value']) === true) {
                 $argString .= '=' . (string) $arg['value'];
             }
         } else {
             $argString = '-' . (string) $arg['value'];
         }
         $cliArgs[] = $argString;
         if (PHP_CODESNIFFER_VERBOSITY > 1) {
             echo str_repeat("\t", $depth);
             echo "\t=> set command line value {$argString}" . PHP_EOL;
         }
     }
     //end foreach
     // Set custom php ini values as CLI args.
     foreach ($ruleset->{'ini'} as $arg) {
         if ($this->_shouldProcessElement($arg) === false) {
             continue;
         }
         if (isset($arg['name']) === false) {
             continue;
         }
         $name = (string) $arg['name'];
         $argString = $name;
         if (isset($arg['value']) === true) {
             $value = (string) $arg['value'];
             $argString .= "={$value}";
         } else {
             $value = 'true';
         }
         $cliArgs[] = '-d';
         $cliArgs[] = $argString;
         if (PHP_CODESNIFFER_VERBOSITY > 1) {
             echo str_repeat("\t", $depth);
             echo "\t=> set PHP ini value {$name} to {$value}" . PHP_EOL;
         }
     }
     //end foreach
     if (empty($cliValues['files']) === true) {
         // Process hard-coded file paths.
         foreach ($ruleset->{'file'} as $file) {
             $file = (string) $file;
             $cliArgs[] = $file;
             if (PHP_CODESNIFFER_VERBOSITY > 1) {
                 echo str_repeat("\t", $depth);
                 echo "\t=> added \"{$file}\" to the file list" . PHP_EOL;
             }
         }
     }
     if (empty($cliArgs) === false) {
         $this->cli->setCommandLineValues($cliArgs);
     }
     // Process custom sniff config settings.
     foreach ($ruleset->{'config'} as $config) {
         if ($this->_shouldProcessElement($config) === false) {
             continue;
         }
         $this->setConfigData((string) $config['name'], (string) $config['value'], true);
         if (PHP_CODESNIFFER_VERBOSITY > 1) {
             echo str_repeat("\t", $depth);
             echo "\t=> set config value " . (string) $config['name'] . ': ' . (string) $config['value'] . PHP_EOL;
         }
     }
     // Process custom ignore pattern rules.
     foreach ($ruleset->{'exclude-pattern'} as $pattern) {
         if ($this->_shouldProcessElement($pattern) === false) {
             continue;
         }
         if (isset($pattern['type']) === false) {
             $pattern['type'] = 'absolute';
         }
         $this->ignorePatterns[(string) $pattern] = (string) $pattern['type'];
         if (PHP_CODESNIFFER_VERBOSITY > 1) {
             echo str_repeat("\t", $depth);
             echo "\t=> added global " . (string) $pattern['type'] . ' ignore pattern: ' . (string) $pattern . PHP_EOL;
         }
     }
     $includedSniffs = array_unique(array_merge($ownSniffs, $includedSniffs));
     $excludedSniffs = array_unique($excludedSniffs);
     if (PHP_CODESNIFFER_VERBOSITY > 1) {
         $included = count($includedSniffs);
         $excluded = count($excludedSniffs);
         echo str_repeat("\t", $depth);
         echo "=> Ruleset processing complete; included {$included} sniffs and excluded {$excluded}" . PHP_EOL;
     }
     // Merge our own sniff list with our externally included
     // sniff list, but filter out any excluded sniffs.
     $files = array();
     foreach ($includedSniffs as $sniff) {
         if (in_array($sniff, $excludedSniffs) === true) {
             continue;
         } else {
             $files[] = self::realpath($sniff);
         }
     }
     return $files;
 }