/** * @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); }
/** * @param CheckMetadata $checkMetadata * @param SplFileInfo $fileInfo * @param bool $useRelativePaths */ protected function checkFile(CheckMetadata $checkMetadata, SplFileInfo $fileInfo, $useRelativePaths) { $context = new FileContext($fileInfo, $useRelativePaths); $this->contextChecker->checkContext($context); if ($context->hasMessagesOrErrors()) { $this->resultPrinter->printContext($context); } $checkMetadata->incrementCheckedFileCount(); }
/** * @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(); }