|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Functions; |
| 4 | + |
| 5 | +use Generator; |
| 6 | +use IteratorAggregate; |
| 7 | +use Override; |
| 8 | +use PHPStan\Php\PhpVersion; |
| 9 | +use PHPStan\Testing\PHPStanTestCase; |
| 10 | +use PHPStan\DependencyInjection\ContainerFactory; |
| 11 | +use PHPStan\Testing\PHPStanTestCaseTrait; |
| 12 | +use PHPStan\Testing\PHPUnit\ContainerInitializer; |
| 13 | +use PHPStan\Testing\PHPUnit\InitContainerBeforeTestSubscriber; |
| 14 | +use PHPUnit\Framework\Attributes\CoversMethod; |
| 15 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 16 | +use PHPUnit\Framework\Attributes\RequiresPhp; |
| 17 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 18 | +use PHPUnit\Framework\Attributes\UsesTrait; |
| 19 | +use Throwable; |
| 20 | +use ValueError; |
| 21 | +use function max; |
| 22 | +use function min; |
| 23 | +use function range; |
| 24 | +use function sprintf; |
| 25 | +use function sscanf; |
| 26 | +use function strlen; |
| 27 | +use const PHP_INT_MAX; |
| 28 | +use const PHP_VERSION_ID; |
| 29 | + |
| 30 | +#[CoversMethod(PrintfHelper::class, 'getScanfPlaceholdersCount')] |
| 31 | +#[UsesClass(PhpVersion::class)] |
| 32 | +#[UsesClass(PrintfHelper::class)] |
| 33 | +#[UsesClass(ContainerFactory::class)] |
| 34 | +#[UsesTrait(PHPStanTestCaseTrait::class)] |
| 35 | +#[UsesClass(ContainerInitializer::class)] |
| 36 | +#[UsesClass(InitContainerBeforeTestSubscriber::class)] |
| 37 | +class PrintfHelperTest extends PHPStanTestCase |
| 38 | +{ |
| 39 | + |
| 40 | + private PrintfHelper $printf; |
| 41 | + |
| 42 | + #[Override] |
| 43 | + protected function setUp(): void |
| 44 | + { |
| 45 | + $this->printf = $this->getPhpVersionIdAwareHelper(PHP_VERSION_ID); |
| 46 | + } |
| 47 | + |
| 48 | + #[RequiresPhp('< 8.0.0')] |
| 49 | + public function testReturnsNullForInvalidPatternOnLegacyPhpVersion(): void |
| 50 | + { |
| 51 | + $this->assertNull($this->printf->getScanfPlaceholdersCount('%a')); |
| 52 | + } |
| 53 | + |
| 54 | + #[RequiresPhp('>= 8.0.0')] |
| 55 | + public function testReturnsNullForInvalidPatternOnPhp8(): void |
| 56 | + { |
| 57 | + $this->assertNull($this->printf->getScanfPlaceholdersCount('%a')); |
| 58 | + } |
| 59 | + |
| 60 | + #[RequiresPhp('>= 8.0.0')] |
| 61 | + #[DataProvider('dataLegacyVersionIds')] |
| 62 | + public function testLegacyVersionStillThrowsValueErrorOnPhp8(int $versionId): void |
| 63 | + { |
| 64 | + $helper = $this->getPhpVersionIdAwareHelper($versionId); |
| 65 | + $this->expectException(ValueError::class); |
| 66 | + $this->expectExceptionMessage('Bad scan conversion character "a"'); |
| 67 | + $helper->getScanfPlaceholdersCount('%a'); |
| 68 | + $this->fail('check your phpunit'); |
| 69 | + } |
| 70 | + |
| 71 | + private function getPhpVersionIdAwareHelper(int $versionId): PrintfHelper |
| 72 | + { |
| 73 | + return new PrintfHelper($this->getPhpVersion($versionId)); |
| 74 | + } |
| 75 | + |
| 76 | + private function getPhpVersion(int $versionId): PhpVersion |
| 77 | + { |
| 78 | + return new PhpVersion($versionId); |
| 79 | + } |
| 80 | + |
| 81 | + public static function dataLegacyVersionIds(): Generator |
| 82 | + { |
| 83 | + static $line; |
| 84 | + $line ??= new class () implements IteratorAggregate { |
| 85 | + |
| 86 | + /** @var array<int, array<int, int>> */ |
| 87 | + private readonly array $base; |
| 88 | + |
| 89 | + /** @var array<int, int> */ |
| 90 | + private readonly array $major; |
| 91 | + |
| 92 | + /** @var array<int, int> */ |
| 93 | + private readonly array $release; |
| 94 | + |
| 95 | + /** |
| 96 | + * @return Generator<int,array{int, int, int}, void, void> |
| 97 | + */ |
| 98 | + #[Override] |
| 99 | + public function getIterator(): Generator |
| 100 | + { |
| 101 | + foreach ($this->major as $major) { |
| 102 | + $minors = $this->base[$major]; |
| 103 | + foreach ($minors as $minor) { |
| 104 | + foreach ($this->release as $release) { |
| 105 | + yield [$major, $minor, $release]; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public function __construct() |
| 112 | + { |
| 113 | + $this->major = range(4, 9); |
| 114 | + $this->release = range(0, 48); |
| 115 | + /** |
| 116 | + * @var array<int, array<int, int>> |
| 117 | + */ |
| 118 | + $base = [ |
| 119 | + 4 => [3, 4], |
| 120 | + 5 => range(0, 6), |
| 121 | + 6 => [], |
| 122 | + 7 => range(0, 4), |
| 123 | + ]; |
| 124 | + foreach ($this->major as $major) { |
| 125 | + if (isset($base[$major])) { |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + // 8: 0...48, 9: 0...36, 10: 0...24, 11...: 0...12 |
| 130 | + $base[$major] = range(0, max(1, -1 * ($major + -12)) * 12); |
| 131 | + } |
| 132 | + $this->base = $base; |
| 133 | + } |
| 134 | + |
| 135 | + }; |
| 136 | + |
| 137 | + $errored = 0; |
| 138 | + $skipped = 0; |
| 139 | + $matched = 0; |
| 140 | + $expectSkipped = 4214; |
| 141 | + $expectMatched = 686; |
| 142 | + |
| 143 | + foreach ($line as [$major, $minor, $release]) { |
| 144 | + $triplet = [max(1, $major), max(0, min(99, $minor)), max(0, min(99, $release))]; |
| 145 | + $strval = sprintf('%d%02d%02d', ...$triplet); |
| 146 | + $ret = sscanf($strval, '%d%n', $intval, $len); |
| 147 | + self::assertSame(2, $ret, 'check your base'); |
| 148 | + self::assertIsInt($intval, 'check %d specifier php manual and sscanf sources'); |
| 149 | + self::assertSame(strlen($strval), $len, 'check string: ' . $strval . ' which is of len ' . strlen($strval)); |
| 150 | + self::assertNotSame(PHP_INT_MAX, $intval, 'IntMax ceiling hit'); |
| 151 | + self::assertGreaterThan(0, $intval, 'UnsignedInt floor hit'); |
| 152 | + $self ??= new PrintfHelperTest(__METHOD__); |
| 153 | + try { |
| 154 | + $version = $self->getPhpVersion($intval); |
| 155 | + } catch (Throwable) { |
| 156 | + $errored++; |
| 157 | + continue; |
| 158 | + } |
| 159 | + $throws = $version->throwsValueErrorForInternalFunctions(); |
| 160 | + if ($throws) { |
| 161 | + $skipped++; |
| 162 | + continue; |
| 163 | + } |
| 164 | + $matched++; |
| 165 | + $i ??= 0; |
| 166 | + $i++; |
| 167 | + $case = sprintf('case #%02d php %d.%d.%d [version-id %s]', $i, $major, $minor, $release, $strval); |
| 168 | + yield $case => [$intval]; |
| 169 | + } |
| 170 | + |
| 171 | + self::assertSame(0, $errored); |
| 172 | + self::assertSame($expectSkipped, $skipped); |
| 173 | + self::assertSame($expectMatched, $matched); |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments