From cdd1672ea359ba937e2874e82f4b67882a56616b Mon Sep 17 00:00:00 2001 From: Siad Ardroumli Date: Wed, 17 Jan 2018 16:50:13 +0100 Subject: [PATCH] Added iterator usage (#835) --- classes/phing/tasks/ext/TarFileSet.php | 11 ++-- classes/phing/tasks/ext/TarTask.php | 10 ++-- classes/phing/tasks/ext/ZipFileSet.php | 13 +++-- classes/phing/tasks/ext/ZipTask.php | 8 +-- classes/phing/tasks/ext/phar/PharDataTask.php | 5 +- .../phing/tasks/ext/phar/PharPackageTask.php | 2 +- .../ext/zendguard/ZendGuardEncodeTask.php | 2 +- classes/phing/types/AbstractFileSet.php | 11 +++- classes/phing/types/DirSet.php | 4 +- classes/phing/types/FileSet.php | 14 +++-- classes/phing/types/IterableFileSet.php | 54 ------------------- classes/phing/util/SourceFileScanner.php | 2 +- .../phing/tasks/ext/ZipUnzipTaskTest.php | 2 +- 13 files changed, 52 insertions(+), 86 deletions(-) diff --git a/classes/phing/tasks/ext/TarFileSet.php b/classes/phing/tasks/ext/TarFileSet.php index 676143b1b4..23b03c3aec 100644 --- a/classes/phing/tasks/ext/TarFileSet.php +++ b/classes/phing/tasks/ext/TarFileSet.php @@ -26,15 +26,16 @@ class TarFileSet extends FileSet * @param Project $p * @param bool $includeEmpty * - * @throws BuildException - * * @return array a list of file and directory names, relative to * the baseDir for the project. + * + * @throws BuildException + * @throws Exception */ - public function getFiles(Project $p, $includeEmpty = true) + protected function getFiles($includeEmpty = true, ...$options) { if ($this->files === null) { - $ds = $this->getDirectoryScanner($p); + $ds = $this->getDirectoryScanner($this->getProject()); $this->files = $ds->getIncludedFiles(); if ($includeEmpty) { @@ -64,7 +65,7 @@ public function getFiles(Project $p, $includeEmpty = true) // to the files array. foreach ($incDirs as $dir) { // we cannot simply use array_diff() since we want to disregard empty/. dirs - if ($dir != "" && $dir != "." && !in_array($dir, $implicitDirs)) { + if ($dir != "" && $dir !== "." && !in_array($dir, $implicitDirs)) { // it's an empty dir, so we'll add it. $this->files[] = $dir; } diff --git a/classes/phing/tasks/ext/TarTask.php b/classes/phing/tasks/ext/TarTask.php index ffc4e28d03..b83fe41b62 100644 --- a/classes/phing/tasks/ext/TarTask.php +++ b/classes/phing/tasks/ext/TarTask.php @@ -246,12 +246,12 @@ public function main() $tar = new Archive_Tar($this->tarFile->getAbsolutePath(), $this->compression); $pear = new PEAR(); - if ($pear->isError($tar->error_object)) { + if ($pear::isError($tar->error_object)) { throw new BuildException($tar->error_object->getMessage()); } foreach ($this->filesets as $fs) { - $files = $fs->getFiles($this->project, $this->includeEmpty); + $files = $fs->getIterator($this->includeEmpty); if (count($files) > 1 && strlen($fs->getFullpath()) > 0) { throw new BuildException("fullpath attribute may only " . "be specified for " @@ -267,7 +267,7 @@ public function main() } $tar->addModify($filesToTar, $this->prefix, $fsBasedir->getAbsolutePath()); - if ($pear->isError($tar->error_object)) { + if ($pear::isError($tar->error_object)) { throw new BuildException($tar->error_object->getMessage()); } } @@ -281,7 +281,7 @@ public function main() } /** - * @param array $files array of filenames + * @param ArrayIterator $files array of filenames * @param PhingFile $dir * * @return boolean @@ -302,7 +302,7 @@ protected function areFilesUpToDate($files, $dir) private function isArchiveUpToDate() { foreach ($this->filesets as $fs) { - $files = $fs->getFiles($this->project, $this->includeEmpty); + $files = $fs->getIterator($this->includeEmpty); if (!$this->areFilesUpToDate($files, $fs->getDir($this->project))) { return false; } diff --git a/classes/phing/tasks/ext/ZipFileSet.php b/classes/phing/tasks/ext/ZipFileSet.php index 148b2f2806..f72ef982b4 100644 --- a/classes/phing/tasks/ext/ZipFileSet.php +++ b/classes/phing/tasks/ext/ZipFileSet.php @@ -31,16 +31,19 @@ class ZipFileSet extends FileSet /** * Get a list of files and directories specified in the fileset. - * @param Project $p + * * @param bool $includeEmpty - * @throws BuildException + * @param array ...$options + * * @return array a list of file and directory names, relative to * the baseDir for the project. + * + * @throws Exception */ - public function getFiles(Project $p, $includeEmpty = true) + protected function getFiles($includeEmpty = true, ...$options) { if ($this->files === null) { - $ds = $this->getDirectoryScanner($p); + $ds = $this->getDirectoryScanner($this->getProject()); $this->files = $ds->getIncludedFiles(); // build a list of directories implicitly added by any of the files @@ -71,7 +74,7 @@ public function getFiles(Project $p, $includeEmpty = true) // to the files array. foreach ($incDirs as $dir) { // we cannot simply use array_diff() since we want to disregard empty/. dirs - if ($dir != "" && $dir != "." && !in_array($dir, $implicitDirs)) { + if ($dir != "" && $dir !== "." && !in_array($dir, $implicitDirs)) { // it's an empty dir, so we'll add it. $emptyDirectories[] = $dir; } diff --git a/classes/phing/tasks/ext/ZipTask.php b/classes/phing/tasks/ext/ZipTask.php index a12aae66e3..d8fcaf6cb1 100644 --- a/classes/phing/tasks/ext/ZipTask.php +++ b/classes/phing/tasks/ext/ZipTask.php @@ -245,7 +245,7 @@ public function areFilesetsUpToDate() { /** @var FileSet $fs */ foreach ($this->filesets as $fs) { - $files = $fs->getFiles($this->project, $this->includeEmpty); + $files = $fs->getIterator($this->includeEmpty); if (!$this->archiveIsUpToDate($files, $fs->getDir($this->project))) { return false; } @@ -267,10 +267,10 @@ private function addFilesetsToArchive($zip) $fsBasedir = (null != $this->baseDir) ? $this->baseDir : $fs->getDir($this->project); - $files = $fs->getFiles($this->project, $this->includeEmpty); + $files = $fs->getIterator($this->includeEmpty); - for ($i = 0, $fcount = count($files); $i < $fcount; $i++) { - $f = new PhingFile($fsBasedir, $files[$i]); + foreach ($files as $file) { + $f = new PhingFile($fsBasedir, $file); $pathInZip = $this->prefix . $f->getPathWithoutBase($fsBasedir); diff --git a/classes/phing/tasks/ext/phar/PharDataTask.php b/classes/phing/tasks/ext/phar/PharDataTask.php index 0cb1148d14..376d0bac6a 100644 --- a/classes/phing/tasks/ext/phar/PharDataTask.php +++ b/classes/phing/tasks/ext/phar/PharDataTask.php @@ -19,7 +19,6 @@ */ require_once 'phing/tasks/system/MatchingTask.php'; -require_once 'phing/types/IterableFileSet.php'; /** * Data task for {@link http://php.net/manual/en/class.phardata.php PharData class}. @@ -47,7 +46,7 @@ class PharDataTask extends MatchingTask private $baseDirectory; /** - * @var IterableFileSet[] + * @var FileSet[] */ private $filesets = []; @@ -56,7 +55,7 @@ class PharDataTask extends MatchingTask */ public function createFileSet() { - $this->fileset = new IterableFileSet(); + $this->fileset = new FileSet(); $this->filesets[] = $this->fileset; return $this->fileset; } diff --git a/classes/phing/tasks/ext/phar/PharPackageTask.php b/classes/phing/tasks/ext/phar/PharPackageTask.php index c629f01509..efb496c154 100644 --- a/classes/phing/tasks/ext/phar/PharPackageTask.php +++ b/classes/phing/tasks/ext/phar/PharPackageTask.php @@ -106,7 +106,7 @@ public function createMetadata() */ public function createFileSet() { - $this->fileset = new IterableFileSet(); + $this->fileset = new FileSet(); $this->filesets[] = $this->fileset; return $this->fileset; diff --git a/classes/phing/tasks/ext/zendguard/ZendGuardEncodeTask.php b/classes/phing/tasks/ext/zendguard/ZendGuardEncodeTask.php index df68efb563..eda3598288 100644 --- a/classes/phing/tasks/ext/zendguard/ZendGuardEncodeTask.php +++ b/classes/phing/tasks/ext/zendguard/ZendGuardEncodeTask.php @@ -401,7 +401,7 @@ public function main() /* @var $fsBasedir PhingFile */ $fsBasedir = $fs->getDir($this->project)->getAbsolutePath(); - $files = $fs->getFiles($this->project, false); + $files = $fs->getIterator(false); foreach ($files as $file) { $f = new PhingFile($fsBasedir, $file); diff --git a/classes/phing/types/AbstractFileSet.php b/classes/phing/types/AbstractFileSet.php index 782cd954a7..4247c4f1c1 100644 --- a/classes/phing/types/AbstractFileSet.php +++ b/classes/phing/types/AbstractFileSet.php @@ -498,5 +498,14 @@ public function appendSelector(FileSelector $selector) $this->setChecked(false); } - abstract public function getIterator(); + /** + * @param array ...$options + * @return ArrayIterator + */ + public function getIterator(...$options): \ArrayIterator + { + return new ArrayIterator($this->getFiles($options)); + } + + abstract protected function getFiles(...$options); } diff --git a/classes/phing/types/DirSet.php b/classes/phing/types/DirSet.php index 4746aa66ab..3a06d4cbae 100644 --- a/classes/phing/types/DirSet.php +++ b/classes/phing/types/DirSet.php @@ -1,5 +1,4 @@ getDirectoryScanner($this->getProject())->getIncludedDirectories(); } diff --git a/classes/phing/types/FileSet.php b/classes/phing/types/FileSet.php index d78db3ecf5..734976cc67 100644 --- a/classes/phing/types/FileSet.php +++ b/classes/phing/types/FileSet.php @@ -37,10 +37,18 @@ class FileSet extends AbstractFileSet { /** * @return array + * @throws Exception */ - public function getIterator() + protected function getFiles(...$options) { - $ds = $this->getDirectoryScanner($this->getProject()); - return $ds->getIncludedFiles() + $ds->getIncludedDirectories(); + $directoryScanner = $this->getDirectoryScanner($this->getProject()); + $files = $directoryScanner->getIncludedFiles(); + + $baseDirectory = $directoryScanner->getBasedir(); + foreach ($files as $index => $file) { + $files[$index] = realpath($baseDirectory . '/' . $file); + } + + return $files; } } diff --git a/classes/phing/types/IterableFileSet.php b/classes/phing/types/IterableFileSet.php index 3613fe70de..e69de29bb2 100644 --- a/classes/phing/types/IterableFileSet.php +++ b/classes/phing/types/IterableFileSet.php @@ -1,54 +0,0 @@ -. - */ - -/** - * FileSet adapter to SPL's Iterator. - * - * @package phing.types - * @author Alexey Shockov - * @since 2.4.0 - * @internal - */ -class IterableFileSet extends FileSet implements IteratorAggregate -{ - /** - * @return Iterator - */ - public function getIterator() - { - return new ArrayIterator($this->getFiles()); - } - - /** - * @return array - */ - private function getFiles() - { - $directoryScanner = $this->getDirectoryScanner($this->getProject()); - $files = $directoryScanner->getIncludedFiles(); - - $baseDirectory = $directoryScanner->getBasedir(); - foreach ($files as $index => $file) { - $files[$index] = realpath($baseDirectory . '/' . $file); - } - - return $files; - } -} diff --git a/classes/phing/util/SourceFileScanner.php b/classes/phing/util/SourceFileScanner.php index da0961f3f4..9b1327bd4f 100644 --- a/classes/phing/util/SourceFileScanner.php +++ b/classes/phing/util/SourceFileScanner.php @@ -49,7 +49,7 @@ public function __construct($task) * Restrict the given set of files to those that are newer than * their corresponding target files. * - * @param array $files the original set of files + * @param iterable $files the original set of files * @param PhingFile $srcDir all files are relative to this directory * @param PhingFile $destDir target files live here. if null file names * returned by the mapper are assumed to be absolute. diff --git a/test/classes/phing/tasks/ext/ZipUnzipTaskTest.php b/test/classes/phing/tasks/ext/ZipUnzipTaskTest.php index df61f9eb12..0568f36840 100644 --- a/test/classes/phing/tasks/ext/ZipUnzipTaskTest.php +++ b/test/classes/phing/tasks/ext/ZipUnzipTaskTest.php @@ -78,6 +78,6 @@ public function testUnzipSimpleZip() $this->executeTarget(__FUNCTION__); $this->assertFileExists($filename); - $this->assertEquals('TEST', file_get_contents($filename)); + $this->assertStringEqualsFile($filename, 'TEST'); } }