php-src icon indicating copy to clipboard operation
php-src copied to clipboard

PHP 8.1.3 *Iterator bug

Open Vermond125 opened this issue 4 years ago • 4 comments

Description

An original weird behavior was found in doctrine/persistence/lib/Doctrine/Persistence/Mapping/Driver/AnnotationDriver.php, but is related to php base classes.

I have 47 files, but this code finds only one of them. To fix this, I can simply put an additional line after the $iterator is defined:

foreach ($iterator as $file) {}

It makes no sence other than internal warmup of some sort, but after it in the next loop I can actually see all 47 files.

The following code:

<?php

// ...
$iterator = new RegexIterator(
    new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
        RecursiveIteratorIterator::LEAVES_ONLY
    ),
    '/^.+' . preg_quote($this->fileExtension) . '$/i',
    RecursiveRegexIterator::GET_MATCH
);

foreach ($iterator as $file) {

Resulted in this output:

1 file found

But I expected this output instead:

47 files found

PHP Version

PHP 8.1.3

Operating System

Ubuntu 20.04.1

Vermond125 avatar Mar 03 '22 16:03 Vermond125

Hi @Vermond125! Can you provide the full code (including the printing of "file found")? And maybe also provide the contents of $path and $this->fileExtension?

I created this test-script and it seems to work fine.

<?php

$file_extension = '.phpt';

$iterator = new RegexIterator(
    new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator(__DIR__, FilesystemIterator::SKIP_DOTS),
        RecursiveIteratorIterator::LEAVES_ONLY
    ),
    '/^.+' . preg_quote($file_extension) . '$/i',
    RecursiveRegexIterator::GET_MATCH
);

var_dump(iterator_to_array($iterator));

iluuu1994 avatar Mar 03 '22 16:03 iluuu1994

Thanx for the fast reply, @iluuu1994!

My bad, I didn't provide full test, because it was coupled with framework and stuff, so here we go:

The following code:

<?php

$path = './Test';
$fileExtension = '.php';

$fileCount = 45;

// Prepare the sandbox
// Remove test sandbox
if (is_dir($path)) {
    array_map('unlink', glob("$path/*.*"));
    
    if (!rmdir($path)) {
        throw new \RuntimeException(sprintf('Cannot recreate directory "%s"', $path));
    }
}

// Create test sandbox
if (!mkdir($path) && !is_dir($path)) {
    throw new \RuntimeException(sprintf('Directory "%s" was not created', $path));
}

// Seed files inside test sandbox
for ($i = 0; $i < $fileCount; $i++) {
    touch(sprintf('%s/%s%s', 
        $path,
        sha1($i), // @note [!] I think it is hard for Iterator to swallow long names 
        $fileExtension)
    );
}

// Main part
$iterator = new RegexIterator(
    new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
        RecursiveIteratorIterator::LEAVES_ONLY
    ),
    '/^.+' . preg_quote($fileExtension) . '$/i',
    RecursiveRegexIterator::GET_MATCH
);

// @note This line is an actual bugfix
// Add this line to get correct result
// foreach ($iterator as $file) {}

// Dummy loop to follow issue's case
$i = 0;
foreach ($iterator as $file) {
    $i++;  
}

echo "$i files found\n";

Resulted in this output:

14 files found

But I expected this output instead:

45 files found

Vermond125 avatar Mar 03 '22 18:03 Vermond125

I ran the script you provided and it worked as expected, with PHP 8.1.3 on Ubuntu. Is there anything else that's special about your configuration? Are you running with OPcache or with JIT? Do you have any special extensions installed? Are there actually 45 elements in your ./Test folder after executing the script?

iluuu1994 avatar Mar 04 '22 09:03 iluuu1994

Checked couple more times and it's definetly local error. Sorry.

Vermond125 avatar Mar 04 '22 12:03 Vermond125