示例#1
0
 /**
  * @param string $path File or directory to check
  * @param string[] $checkedExtensions Only files having these extensions will be checked
  */
 public function check($path, array $checkedExtensions)
 {
     $isPathDir = is_dir($path);
     $isPathFile = is_file($path);
     if (!$isPathDir && !$isPathFile) {
         throw new \InvalidArgumentException(sprintf('Path %s is not a file or a directory', $path));
     }
     if (!$isPathFile && !$checkedExtensions) {
         throw new \DomainException('At least 1 extension should be specified to check a directory');
     }
     $checkMetadata = new CheckMetadata();
     if (is_file($path)) {
         $context = $this->fileContextFactory->createContext($path);
         $this->contextChecker->checkContext($context);
         $this->resultPrinter->printContext($context);
         $checkMetadata->incrementCheckedFileCount();
     } else {
         $directoryIterator = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::KEY_AS_PATHNAME | \RecursiveDirectoryIterator::CURRENT_AS_FILEINFO | \RecursiveDirectoryIterator::SKIP_DOTS);
         $recursiveIterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::LEAVES_ONLY);
         /** @var \SplFileInfo $fileInfo */
         foreach ($recursiveIterator as $pathName => $fileInfo) {
             if (!in_array($fileInfo->getExtension(), $checkedExtensions)) {
                 continue;
             }
             $context = $this->fileContextFactory->createContext($pathName);
             $this->contextChecker->checkContext($context);
             if (count($context->getMessages())) {
                 $this->resultPrinter->printContext($context);
             }
             $checkMetadata->incrementCheckedFileCount();
         }
     }
     $checkMetadata->endCheck();
     $this->resultPrinter->printMetadata($checkMetadata);
 }
示例#2
0
 /**
  * @param CheckMetadata $checkMetadata
  * @param string        $pathName
  */
 protected function checkFile(CheckMetadata $checkMetadata, $pathName)
 {
     $context = $this->fileContextFactory->createContext($pathName);
     $this->contextChecker->checkContext($context);
     if ($context->hasMessagesOrErrors()) {
         $this->resultPrinter->printContext($context);
     }
     $checkMetadata->incrementCheckedFileCount();
 }