Skip to content

Commit ff40d9b

Browse files
committed
Fix compatibility issues
1 parent c8a34c1 commit ff40d9b

File tree

8 files changed

+389
-0
lines changed

8 files changed

+389
-0
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
"autoload": {
5555
"classmap": [
5656
"src/"
57+
],
58+
"files": [
59+
"src/Facade.php",
60+
"src/Factory.php",
61+
"src/Iterator.php"
5762
]
5863
},
5964
"autoload-dev": {

src/Facade.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/*
3+
* This file is part of the File_Iterator package.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
/**
12+
* Façade implementation that uses File_Iterator_Factory to create a
13+
* File_Iterator that operates on an AppendIterator that contains an
14+
* RecursiveDirectoryIterator for each given path. The list of unique
15+
* files is returned as an array.
16+
*
17+
* @since Class available since Release 1.3.0
18+
*/
19+
class File_Iterator_Facade
20+
{
21+
/**
22+
* @param array|string $paths
23+
* @param array|string $suffixes
24+
* @param array|string $prefixes
25+
* @param array $exclude
26+
* @param bool $commonPath
27+
* @return array
28+
*/
29+
public function getFilesAsArray($paths, $suffixes = '', $prefixes = '', array $exclude = array(), $commonPath = FALSE)
30+
{
31+
if (is_string($paths)) {
32+
$paths = array($paths);
33+
}
34+
35+
$factory = new File_Iterator_Factory;
36+
$iterator = $factory->getFileIterator(
37+
$paths, $suffixes, $prefixes, $exclude
38+
);
39+
40+
$files = array();
41+
42+
foreach ($iterator as $file) {
43+
$file = $file->getRealPath();
44+
45+
if ($file) {
46+
$files[] = $file;
47+
}
48+
}
49+
50+
foreach ($paths as $path) {
51+
if (is_file($path)) {
52+
$files[] = realpath($path);
53+
}
54+
}
55+
56+
$files = array_unique($files);
57+
sort($files);
58+
59+
if ($commonPath) {
60+
return array(
61+
'commonPath' => $this->getCommonPath($files),
62+
'files' => $files
63+
);
64+
} else {
65+
return $files;
66+
}
67+
}
68+
69+
/**
70+
* Returns the common path of a set of files.
71+
*
72+
* @param array $files
73+
* @return string
74+
*/
75+
protected function getCommonPath(array $files)
76+
{
77+
$count = count($files);
78+
79+
if ($count == 0) {
80+
return '';
81+
}
82+
83+
if ($count == 1) {
84+
return dirname($files[0]) . DIRECTORY_SEPARATOR;
85+
}
86+
87+
$_files = array();
88+
89+
foreach ($files as $file) {
90+
$_files[] = $_fileParts = explode(DIRECTORY_SEPARATOR, $file);
91+
92+
if (empty($_fileParts[0])) {
93+
$_fileParts[0] = DIRECTORY_SEPARATOR;
94+
}
95+
}
96+
97+
$common = '';
98+
$done = FALSE;
99+
$j = 0;
100+
$count--;
101+
102+
while (!$done) {
103+
for ($i = 0; $i < $count; $i++) {
104+
if ($_files[$i][$j] != $_files[$i+1][$j]) {
105+
$done = TRUE;
106+
break;
107+
}
108+
}
109+
110+
if (!$done) {
111+
$common .= $_files[0][$j];
112+
113+
if ($j > 0) {
114+
$common .= DIRECTORY_SEPARATOR;
115+
}
116+
}
117+
118+
$j++;
119+
}
120+
121+
return DIRECTORY_SEPARATOR . $common;
122+
}
123+
}

src/Factory.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/*
3+
* This file is part of the File_Iterator package.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
/**
12+
* Factory Method implementation that creates a File_Iterator that operates on
13+
* an AppendIterator that contains an RecursiveDirectoryIterator for each given
14+
* path.
15+
*
16+
* @since Class available since Release 1.1.0
17+
*/
18+
class File_Iterator_Factory
19+
{
20+
/**
21+
* @param array|string $paths
22+
* @param array|string $suffixes
23+
* @param array|string $prefixes
24+
* @param array $exclude
25+
* @return AppendIterator
26+
*/
27+
public function getFileIterator($paths, $suffixes = '', $prefixes = '', array $exclude = array())
28+
{
29+
if (is_string($paths)) {
30+
$paths = array($paths);
31+
}
32+
33+
$paths = $this->getPathsAfterResolvingWildcards($paths);
34+
$exclude = $this->getPathsAfterResolvingWildcards($exclude);
35+
36+
if (is_string($prefixes)) {
37+
if ($prefixes != '') {
38+
$prefixes = array($prefixes);
39+
} else {
40+
$prefixes = array();
41+
}
42+
}
43+
44+
if (is_string($suffixes)) {
45+
if ($suffixes != '') {
46+
$suffixes = array($suffixes);
47+
} else {
48+
$suffixes = array();
49+
}
50+
}
51+
52+
$iterator = new AppendIterator;
53+
54+
foreach ($paths as $path) {
55+
if (is_dir($path)) {
56+
$iterator->append(
57+
new File_Iterator(
58+
new RecursiveIteratorIterator(
59+
new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::FOLLOW_SYMLINKS)
60+
),
61+
$suffixes,
62+
$prefixes,
63+
$exclude,
64+
$path
65+
)
66+
);
67+
}
68+
}
69+
70+
return $iterator;
71+
}
72+
73+
/**
74+
* @param array $paths
75+
* @return array
76+
*/
77+
protected function getPathsAfterResolvingWildcards(array $paths)
78+
{
79+
$_paths = array();
80+
81+
foreach ($paths as $path) {
82+
if ($locals = glob($path, GLOB_ONLYDIR)) {
83+
$_paths = array_merge($_paths, array_map('realpath', $locals));
84+
} else {
85+
$_paths[] = realpath($path);
86+
}
87+
}
88+
89+
return $_paths;
90+
}
91+
}

src/Framework/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ public function toString()
304304
*
305305
* @return int
306306
*/
307+
#[\ReturnTypeWillChange]
307308
public function count()
308309
{
309310
return 1;

src/Framework/TestResult.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ public function run(PHPUnit_Framework_Test $test)
738738
*
739739
* @return int
740740
*/
741+
#[\ReturnTypeWillChange]
741742
public function count()
742743
{
743744
return $this->runTests;

src/Framework/TestSuite.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ public function addTestFiles($filenames)
392392
*
393393
* @return int
394394
*/
395+
#[\ReturnTypeWillChange]
395396
public function count($preferCache = false)
396397
{
397398
if ($preferCache && $this->cachedNumTests != null) {
@@ -968,6 +969,7 @@ public function setBackupStaticAttributes($backupStaticAttributes)
968969
*
969970
* @since Method available since Release 3.1.0
970971
*/
972+
#[\ReturnTypeWillChange]
971973
public function getIterator()
972974
{
973975
$iterator = new PHPUnit_Util_TestSuiteIterator($this);

0 commit comments

Comments
 (0)