/** * Main worker * * @return void */ public function main() { if (empty($this->args)) { $this->error("Require at least one file path"); die; } foreach ($this->args as $input) { $path = $this->getPath($input); if (empty($path)) { $this->error("Unable to find {$input}"); die; } if (is_file($path)) { $files = array($path); } if (is_dir($path)) { $dir = new Folder($path); $files = $dir->findRecursive('.*\\.php$'); } if (empty($files)) { $this->error("Invalid file type {$path}"); die; } foreach ($files as $file) { $this->out($file); PhpTidy::file($file); } } }
/** * */ public function testPhpTidyFile_good_single() { // pass in a filename of PHP code to tidy up $noTrailingLine = '<?php /* basic */ ?>'; $path = TMP . 'test-phptidytest-1.php'; file_put_contents($path, $noTrailingLine); $output = PhpTidy::file($path); $this->assertFalse(empty($output)); //$this->assertEquals('Replaced 1 files.', $output); $result = file_get_contents($path); $expect = "<?php /* basic */ ?>\n"; $this->assertEquals($expect, $result); }
/** * Tidy up source code as a file * - executes the phptidy.php script via php CLI functionality * * @param string $path to file * @return void; * @throws OutOfBoundsException */ public static function file($path) { if (is_array($path)) { foreach ($path as $_path) { PhpTidy::file($_path); } return; } if (!is_file($path)) { throw new OutOfBoundsException('PhpTidy::file() unable to find file (should be a full path) ' . $path); } $phpexe = exec('which php'); if (empty($phpexe)) { $phpexe = 'php'; } $cmd = sprintf('%s %s replace %s', $phpexe, escapeshellarg(PhpTidy::script()), escapeshellarg($path)); return exec($cmd); }