Skip to content

Commit

Permalink
fixes issue #301 about conditional code
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Mar 25, 2021
1 parent c4884a5 commit bf5c2a2
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"ext-libxml": "*",
"ext-pcre": "*",
"ext-spl": "*",
"bartlett/php-compatinfo-db": "^3.3",
"bartlett/php-compatinfo-db": "dev-master",
"composer/package-versions-deprecated": "^1.8",
"doctrine/collections": "^1.4",
"nikic/php-parser": "^4.10",
Expand Down
20 changes: 10 additions & 10 deletions src/Application/DataCollector/VersionDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ public function collect(array $nodes): array
$name = $element['id'];
$versions = array_replace(self::$php4, $element['versions']);

if (isset($versions['opt.group'])) {
$this->handleCodeWithCondition($name, $versions);
unset(
$versions['opt.name'],
$versions['opt.group'],
$versions['opt.versions']
);
} else {
$versions['parents'] = $element['parents'];
}
if (isset($this->data[$group][$name])) {
$this->updateElementVersion($this->data[$group][$name], $versions);
} else {
if (isset($versions['opt.group'])) {
$this->handleCodeWithCondition($name, $versions);
unset(
$versions['opt.name'],
$versions['opt.group'],
$versions['opt.versions']
);
} else {
$versions['parents'] = $element['parents'];
}
$this->data[$group][$name] = $versions;
}
}
Expand Down
50 changes: 50 additions & 0 deletions tests/ConditionIssueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
namespace Bartlett\CompatInfo\Tests;

use Exception;
use function array_diff;
use function array_filter;
use function array_keys;
use function version_compare;

/**
* @link https://github.com/llaville/php-compat-info/issues/128
* @link https://github.com/llaville/php-compat-info/issues/159
* @link https://github.com/llaville/php-compat-info/issues/160
* @link https://github.com/llaville/php-compat-info/issues/195
* @link https://github.com/llaville/php-compat-info/issues/301
*/
final class ConditionIssueTest extends TestCase
{
Expand Down Expand Up @@ -183,4 +188,49 @@ public function testRegressionGH195()
$methods['Normalizer\normalize']['ext.max']
);
}

/**
* Regression test for issue #301
*
* @link https://github.com/llaville/php-compat-info/issues/301
* Multiple conditions not displayed
* @group regression
* @return void
* @throws Exception
*/
public function testRegressionGH301()
{
$dataSource = 'gh301';
$metrics = $this->executeAnalysis($dataSource);

$functions = $metrics[self::$analyserId]['functions'];
$functions = array_filter($functions, function($function) {
return ($function['optional'] ?? false) && version_compare($function['ext.min'], '8.0.0alpha1', 'eq');
});
$diff = array_diff(
array_keys($functions),
[
'fdiv',
'preg_last_error_msg',
'str_contains',
'str_starts_with',
'str_ends_with',
'get_debug_type',
'get_resource_id',
]
);
$this->assertCount(0, $diff, 'Conditional functions analysis does not match');

$constants = $metrics[self::$analyserId]['constants'];
$constants = array_filter($constants, function($constant) {
return ($constant['optional'] ?? false) && version_compare($constant['ext.min'], '8.0.0alpha1', 'eq');
});
$diff = array_diff(
array_keys($constants),
[
'FILTER_VALIDATE_BOOL',
]
);
$this->assertCount(0, $diff, 'Conditional constants analysis does not match');
}
}
42 changes: 42 additions & 0 deletions tests/fixtures/conditions/gh301/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\Polyfill\Php80 as p;

if (\PHP_VERSION_ID >= 80000) {
return;
}

if (!defined('FILTER_VALIDATE_BOOL') && defined('FILTER_VALIDATE_BOOLEAN')) {
define('FILTER_VALIDATE_BOOL', \FILTER_VALIDATE_BOOLEAN);
}

if (!function_exists('fdiv')) {
function fdiv(float $num1, float $num2): float { return p\Php80::fdiv($num1, $num2); }
}
if (!function_exists('preg_last_error_msg')) {
function preg_last_error_msg(): string { return p\Php80::preg_last_error_msg(); }
}
if (!function_exists('str_contains')) {
function str_contains(string $haystack, string $needle): bool { return p\Php80::str_contains($haystack, $needle); }
}
if (!function_exists('str_starts_with')) {
function str_starts_with(string $haystack, string $needle): bool { return p\Php80::str_starts_with($haystack, $needle); }
}
if (!function_exists('str_ends_with')) {
function str_ends_with(string $haystack, string $needle): bool { return p\Php80::str_ends_with($haystack, $needle); }
}
if (!function_exists('get_debug_type')) {
function get_debug_type($value): string { return p\Php80::get_debug_type($value); }
}
if (!function_exists('get_resource_id')) {
function get_resource_id($resource): int { return p\Php80::get_resource_id($resource); }
}

0 comments on commit bf5c2a2

Please sign in to comment.