示例#1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->input = $input;
     $this->output = new SymfonyStyle($input, $output);
     try {
         $this->fire();
     } catch (Exception $e) {
         $this->output->error($e->getMessage());
     }
 }
示例#2
0
 /**
  * Process property of unknown type
  *
  * Method warns the user about the unknown type
  * and then uses a fallback; it attempts to just
  * handle the property as if it was of the type
  * 'value'
  *
  * @see handleValueType()
  *
  * @param Property $property
  *
  * @return null|string Property's value
  */
 protected function handleUnknownType(Property $property)
 {
     $message = "Unknown property type '{$property->getType()}' on '{$property->getId()}'." . PHP_EOL;
     $message .= "Using fallback of type 'VALUE' to process property.";
     $this->output->warning($message);
     return $this->handleValueType($property);
 }
示例#3
0
 /**
  * Output execution status information to the console
  *
  * @param OutputInterface|StyleInterface $output
  * @param ConsoleTask $task
  * @param int $number The number of the task to be executed
  * @param int $total The total amount of tasks to be executed
  */
 protected function outputExecutionInfo($output, ConsoleTask $task, $number, $total)
 {
     $taskXofY = "Task ({$number}/{$total})";
     // Special formatting, if Symfony Style is provided
     if ($output instanceof StyleInterface) {
         $output->section($task->getName());
         $output->text($task->getDescription());
         $output->text($taskXofY);
         $output->newLine();
         return;
     }
     // Std. formatting
     $output->writeln($task->getName());
     $output->writeln('--------------------------------');
     $output->writeln($task->getDescription());
     $output->writeln($taskXofY);
     $output->writeln('');
 }
示例#4
0
 /**
  * Write a string as warning output.
  *
  * @param  string  $string
  * @return void
  */
 public function warn($string)
 {
     $this->output->writeln("<warning>{$string}</warning>");
 }
示例#5
0
 /**
  * @param string[] $paths
  * @param \Symfony\Component\Console\Style\StyleInterface $style
  * @param bool $defaultLevelUsed
  * @return int
  */
 public function analyse(array $paths, StyleInterface $style, bool $defaultLevelUsed) : int
 {
     $errors = [];
     $files = [];
     $this->updateMemoryLimitFile();
     foreach ($paths as $path) {
         $realpath = realpath($path);
         if ($realpath === false || !file_exists($realpath)) {
             $errors[] = new Error(sprintf('<error>Path %s does not exist</error>', $path), $path);
         } elseif (is_file($realpath)) {
             $files[] = $realpath;
         } else {
             $finder = new Finder();
             foreach ($finder->files()->name('*.php')->in($realpath) as $fileInfo) {
                 $files[] = $fileInfo->getPathname();
             }
         }
     }
     $this->updateMemoryLimitFile();
     $progressStarted = false;
     $fileOrder = 0;
     $errors = array_merge($errors, $this->analyser->analyse($files, function () use($style, &$progressStarted, $files, &$fileOrder) {
         if (!$progressStarted) {
             $style->progressStart(count($files));
             $progressStarted = true;
         }
         $style->progressAdvance();
         if ($fileOrder % 100 === 0) {
             $this->updateMemoryLimitFile();
         }
         $fileOrder++;
     }));
     if ($progressStarted) {
         $style->progressFinish();
     }
     if (count($errors) === 0) {
         $style->success('No errors');
         if ($defaultLevelUsed) {
             $style->note(sprintf('PHPStan is performing only the most basic checks. You can pass a higher rule level through the --%s option (the default and current level is %d) to analyse code more thoroughly.', AnalyseCommand::OPTION_LEVEL, AnalyseCommand::DEFAULT_LEVEL));
         }
         return 0;
     }
     $currentDir = realpath(dirname($paths[0]));
     $cropFilename = function ($filename) use($currentDir) {
         if ($currentDir !== false && strpos($filename, $currentDir) === 0) {
             return substr($filename, strlen($currentDir) + 1);
         }
         return $filename;
     };
     $fileErrors = [];
     $notFileSpecificErrors = [];
     $totalErrorsCount = count($errors);
     foreach ($errors as $error) {
         if (is_string($error)) {
             $notFileSpecificErrors[] = [$error];
             continue;
         }
         if (!isset($fileErrors[$error->getFile()])) {
             $fileErrors[$error->getFile()] = [];
         }
         $fileErrors[$error->getFile()][] = $error;
     }
     foreach ($fileErrors as $file => $errors) {
         $rows = [];
         foreach ($errors as $error) {
             $rows[] = [(string) $error->getLine(), $error->getMessage()];
         }
         $style->table(['Line', $cropFilename($file)], $rows);
     }
     if (count($notFileSpecificErrors) > 0) {
         $style->table(['Error'], $notFileSpecificErrors);
     }
     $style->error(sprintf($totalErrorsCount === 1 ? 'Found %d error' : 'Found %d errors', $totalErrorsCount));
     return 1;
 }
示例#6
0
 private function setUpSignalHandler(StyleInterface $consoleStyle, string $memoryLimitFile)
 {
     if (function_exists('pcntl_signal')) {
         pcntl_signal(SIGINT, function () use($consoleStyle, $memoryLimitFile) {
             if (file_exists($memoryLimitFile)) {
                 unlink($memoryLimitFile);
             }
             $consoleStyle->newLine();
             exit(1);
         });
     }
 }
示例#7
0
 public function ask($question, $regex, $errorText = null, $default = null)
 {
     return $this->output->ask("<question>{$question}</question>", $default, $this->validateWith($regex, $errorText));
 }
示例#8
0
 /**
  * @param string[] $paths
  * @param \Symfony\Component\Console\Style\StyleInterface $style
  * @return int
  */
 public function analyse(array $paths, StyleInterface $style) : int
 {
     $errors = [];
     $files = [];
     foreach ($paths as $path) {
         $realpath = realpath($path);
         if ($realpath === false || !file_exists($realpath)) {
             $errors[] = new Error(sprintf('<error>Path %s does not exist</error>', $path), $path);
         } elseif (is_file($realpath)) {
             $files[] = $realpath;
         } else {
             $finder = new Finder();
             foreach ($finder->files()->name('*.php')->in($realpath) as $fileInfo) {
                 $files[] = $fileInfo->getPathname();
             }
         }
     }
     $progressStarted = false;
     $errors = array_merge($errors, $this->analyser->analyse($files, function () use($style, &$progressStarted, $files) {
         if (!$progressStarted) {
             $style->progressStart(count($files));
             $progressStarted = true;
         }
         $style->progressAdvance();
     }));
     if ($progressStarted) {
         $style->progressFinish();
     }
     if (count($errors) === 0) {
         $style->success('No errors');
         return 0;
     }
     $currentDir = realpath(dirname($paths[0]));
     $cropFilename = function ($filename) use($currentDir) {
         if ($currentDir !== false && strpos($filename, $currentDir) === 0) {
             return substr($filename, strlen($currentDir) + 1);
         }
         return $filename;
     };
     $fileErrors = [];
     $notFileSpecificErrors = [];
     $totalErrorsCount = count($errors);
     foreach ($errors as $error) {
         if (is_string($error)) {
             $notFileSpecificErrors[] = [$error];
             continue;
         }
         if (!isset($fileErrors[$error->getFile()])) {
             $fileErrors[$error->getFile()] = [];
         }
         $fileErrors[$error->getFile()][] = $error;
     }
     foreach ($fileErrors as $file => $errors) {
         $rows = [];
         foreach ($errors as $error) {
             $rows[] = [(string) $error->getLine(), $error->getMessage()];
         }
         $style->table(['Line', $cropFilename($file)], $rows);
     }
     if (count($notFileSpecificErrors) > 0) {
         $style->table(['Error'], $notFileSpecificErrors);
     }
     $style->error(sprintf($totalErrorsCount === 1 ? 'Found %d error' : 'Found %d errors', $totalErrorsCount));
     return 1;
 }
示例#9
0
 /**
  * @param StyleInterface $out
  * @param Bookmark[]     $bookmarks
  * @param array          $extra
  */
 public static function table(StyleInterface $out, $bookmarks, $extra = array())
 {
     $headers = array_merge(self::getTableHeaders(), $extra);
     $out->table($headers, self::getTableRows($bookmarks, $extra));
 }