protected function execute(InputInterface $input, OutputInterface $output) { // now we are ready to start working // let's have fun finding all the files to parse $files = array(); $found = false; $this->_getFiles($input->getOption('input-path'), $files, $input->getOption('recursive')); $wtfs = new Wtfs(); $params = array('outputPath' => $input->getOption('output-path'), 'format' => $input->getOption('format')); $report = new Report($params); // foreach file we should check if it is an amd one, if so, // get the dependency list, and check the code. $start = microtime(true); foreach ($files as $file) { // Get a file into an array. // In this example we'll go through HTTP to get // the HTML source of a URL. $lines = file($file); //, FILE_IGNORE_NEW_LINES // Loop through our array, show HTML source as HTML source; // and line numbers too. $startFound = false; $endFound = false; $errorMsg = ''; $lineStart = 1; $wtfsInFile = array(); foreach ($lines as $lineNb => $line) { $wtfStart = stripos($line, '@wtf_start'); $displayLine = $lineNb + 1; if ($wtfStart !== false && !$startFound) { $lineStart = $displayLine; // lines start with 1, not 0 $startFound = true; } elseif ($wtfStart !== false && !!$startFound) { // we found a @wtf_start inside another @wtf_start snippet // we do not support nested wtfs, come on... $snippet = new Snippet($lineStart . '-noEnd'); $snippet->setLineStart($lineStart); $snippet->setSeverity('error'); $snippet->setSnippet('A @wtf_start has been found without a matching @wtf_stop in ' . $file . ' at line ' . $lineStart); $wtfsInFile[$lineStart] = $snippet; $found = true; // proceed to the next snippet directly $lineStart = $displayLine; $startFound = true; } $wtfEnd = stripos($line, '@wtf_stop'); if ($wtfEnd !== false) { $endFound = true; $startFound = false; } if ($startFound && !$endFound || !$startFound && $endFound) { $errorMsg .= $line; $found = true; if ($endFound) { $snippet = new Snippet($lineStart . '-' . $displayLine); $snippet->setLineStart($lineStart); $snippet->setLineStop($displayLine); $snippet->setSeverity('error'); $snippet->setSnippet($errorMsg); $wtfsInFile[$displayLine] = $snippet; $endFound = false; $errorMsg = ''; } } } if ($startFound) { // we have a problem here, it means that someone has put a // wtf_start without a wtf_stop. // Rather than reporting the whole file, we will trigger an // exception or just report the line where the wtf_start was if skip-error option was set. $message = 'A @wtf_start has been found without a matching @wtf_stop in ' . $file . ' at line ' . $lineStart; if (!$input->getOption('skip-error')) { throw new \Exception($message); } else { $snippet = new Snippet($lineStart . '-noEnd'); $snippet->setLineStart($lineStart); $snippet->setSeverity('error'); $snippet->setSnippet('A @wtf_start has been found without a matching @wtf_stop in ' . $file . ' at line ' . $lineStart); $wtfsInFile[$lineStart] = $snippet; } } if ($found) { $wtfs->addWtf(new Wtf(array('file' => $file, 'wtfsnippets' => $wtfsInFile))); $found = false; } } $middle = microtime(true); $report->generateReport($wtfs); $end = microtime(true); $output->writeln('<info>Work Done!</info>'); if ($input->getOption('bench')) { $this->_displayBench(count($files), $start, $middle, $end, $output); } }
/** * Add a wtf to the wtf list * @param \Phpwtf\WtfSnippet $snippet * @throws \Exception */ public function addWtfSnippet(Snippet $snippet) { $wtfs = $this->getWtfSnippets(); $identifier = $snippet->getIdentifier(); if (empty($wtfs[$identifier])) { $this->_wtfSnippets[$identifier] = $snippet; } else { throw new \Exception('Snippet already reported'); } }