Skip to content

Commit

Permalink
Added iterator usage
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 committed Dec 30, 2017
1 parent 18681b2 commit 18d511f
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 88 deletions.
11 changes: 6 additions & 5 deletions classes/phing/tasks/ext/TarFileSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
10 changes: 5 additions & 5 deletions classes/phing/tasks/ext/TarTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,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 "
Expand All @@ -272,7 +272,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());
}
}
Expand All @@ -286,7 +286,7 @@ public function main()
}

/**
* @param array $files array of filenames
* @param ArrayIterator $files array of filenames
* @param PhingFile $dir
*
* @return boolean
Expand All @@ -307,7 +307,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;
}
Expand Down
13 changes: 8 additions & 5 deletions classes/phing/tasks/ext/ZipFileSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions classes/phing/tasks/ext/ZipTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,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;
}
Expand All @@ -268,10 +268,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);
Expand Down
5 changes: 2 additions & 3 deletions classes/phing/tasks/ext/phar/PharDataTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,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}.
Expand Down Expand Up @@ -48,7 +47,7 @@ class PharDataTask extends MatchingTask
private $baseDirectory;

/**
* @var IterableFileSet[]
* @var FileSet[]
*/
private $filesets = [];

Expand All @@ -57,7 +56,7 @@ class PharDataTask extends MatchingTask
*/
public function createFileSet()
{
$this->fileset = new IterableFileSet();
$this->fileset = new FileSet();
$this->filesets[] = $this->fileset;
return $this->fileset;
}
Expand Down
3 changes: 1 addition & 2 deletions classes/phing/tasks/ext/phar/PharPackageTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/

require_once 'phing/tasks/system/MatchingTask.php';
require_once 'phing/types/IterableFileSet.php';
require_once 'phing/tasks/ext/phar/PharMetadata.php';

/**
Expand Down Expand Up @@ -111,7 +110,7 @@ public function createMetadata()
*/
public function createFileSet()
{
$this->fileset = new IterableFileSet();
$this->fileset = new FileSet();
$this->filesets[] = $this->fileset;

return $this->fileset;
Expand Down
2 changes: 1 addition & 1 deletion classes/phing/tasks/ext/zendguard/ZendGuardEncodeTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,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);
Expand Down
11 changes: 10 additions & 1 deletion classes/phing/types/AbstractFileSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,5 +531,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);
}
4 changes: 2 additions & 2 deletions classes/phing/types/DirSet.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/*
* $Id$
*
Expand Down Expand Up @@ -32,8 +31,9 @@ class DirSet extends AbstractFileSet
{
/**
* @return array
* @throws Exception
*/
public function getIterator()
protected function getFiles(...$options)
{
return $this->getDirectoryScanner($this->getProject())->getIncludedDirectories();
}
Expand Down
14 changes: 11 additions & 3 deletions classes/phing/types/FileSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,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;
}
}
55 changes: 0 additions & 55 deletions classes/phing/types/IterableFileSet.php

This file was deleted.

2 changes: 1 addition & 1 deletion classes/phing/util/SourceFileScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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.
Expand Down
2 changes: 1 addition & 1 deletion test/classes/phing/tasks/ext/ZipUnzipTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ public function testUnzipSimpleZip()
$this->executeTarget(__FUNCTION__);

$this->assertFileExists($filename);
$this->assertEquals('TEST', file_get_contents($filename));
$this->assertStringEqualsFile($filename, 'TEST');
}
}

0 comments on commit 18d511f

Please sign in to comment.