Skip to content

Commit 654637d

Browse files
author
Simon Mönch
authored
Fix internal constants (#425)
1 parent 9afbe0f commit 654637d

File tree

10 files changed

+143
-52
lines changed

10 files changed

+143
-52
lines changed

composer.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

specs/exp/coalescing-assignment.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
return [
1616
'meta' => [
17+
'minPhpVersion' => 70400,
1718
'title' => 'Null coalescing assignment operator',
1819
// Default values. If not specified will be the one used
1920
'prefix' => 'Humbug',

specs/func-arrow/global-scope-arrow-func.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
return [
1616
'meta' => [
17+
'minPhpVersion' => 70400,
1718
'title' => 'Arrow function in the global namespace',
1819
// Default values. If not specified will be the one used
1920
'prefix' => 'Humbug',

specs/func-arrow/namespace-arrow-func-with-use-stmt.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
return [
1616
'meta' => [
17+
'minPhpVersion' => 70400,
1718
'title' => 'Arrow function in a namespace with use statements',
1819
// Default values. If not specified will be the one used
1920
'prefix' => 'Humbug',

specs/func-arrow/namespace-arrow-func.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
return [
1616
'meta' => [
17+
'minPhpVersion' => 70400,
1718
'title' => 'Arrow function in a namespace',
1819
// Default values. If not specified will be the one used
1920
'prefix' => 'Humbug',

specs/misc/match.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
return [
16+
'meta' => [
17+
'title' => 'Match',
18+
// Default values. If not specified will be the one used
19+
'prefix' => 'Humbug',
20+
'whitelist' => [],
21+
'whitelist-global-constants' => false,
22+
'whitelist-global-classes' => false,
23+
'whitelist-global-functions' => false,
24+
'registered-classes' => [],
25+
'registered-functions' => [],
26+
],
27+
'match' => <<<'PHP'
28+
<?php declare(strict_types=1);
29+
30+
namespace Acme {
31+
use Acme\Foo\Match;
32+
33+
class Foo implements Match {}
34+
}
35+
36+
namespace Acme\Foo {
37+
interface Match {}
38+
}
39+
40+
----
41+
<?php
42+
43+
declare (strict_types=1);
44+
namespace Humbug\Acme;
45+
46+
use Humbug\Acme\Foo\Match;
47+
class Foo implements \Humbug\Acme\Foo\Match
48+
{
49+
}
50+
namespace Humbug\Acme\Foo;
51+
52+
interface Match
53+
{
54+
}
55+
56+
PHP
57+
];

src/Container.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Humbug\PhpScoper\Scoper\PatchScoper;
2222
use Humbug\PhpScoper\Scoper\PhpScoper;
2323
use Humbug\PhpScoper\Scoper\SymfonyScoper;
24+
use PhpParser\Lexer;
2425
use PhpParser\Parser;
2526
use PhpParser\ParserFactory;
2627

@@ -54,7 +55,7 @@ public function getScoper(): Scoper
5455
public function getParser(): Parser
5556
{
5657
if (null === $this->parser) {
57-
$this->parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
58+
$this->parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7, new Lexer());
5859
}
5960

6061
return $this->parser;

src/Reflector.php

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
namespace Humbug\PhpScoper;
1616

1717
use JetBrains\PHPStormStub\PhpStormStubsMap;
18+
use function array_diff;
1819
use function array_fill_keys;
20+
use function array_filter;
1921
use function array_keys;
2022
use function array_merge;
2123
use function strtolower;
24+
use const PHP_VERSION_ID;
2225

2326
/**
2427
* @private
@@ -27,55 +30,55 @@ final class Reflector
2730
{
2831
private const MISSING_CLASSES = [
2932
// https://github.com/JetBrains/phpstorm-stubs/pull/594
30-
'parallel\Channel',
31-
'parallel\Channel\Error',
32-
'parallel\Channel\Error\Closed',
33-
'parallel\Channel\Error\Existence',
34-
'parallel\Channel\Error\IllegalValue',
35-
'parallel\Error',
36-
'parallel\Events',
37-
'parallel\Events\Error',
38-
'parallel\Events\Error\Existence',
39-
'parallel\Events\Error\Timeout',
40-
'parallel\Events\Event',
41-
'parallel\Events\Event\Type',
42-
'parallel\Events\Input',
43-
'parallel\Events\Input\Error',
44-
'parallel\Events\Input\Error\Existence',
45-
'parallel\Events\Input\Error\IllegalValue',
46-
'parallel\Future',
47-
'parallel\Future\Error',
48-
'parallel\Future\Error\Cancelled',
49-
'parallel\Future\Error\Foreign',
50-
'parallel\Future\Error\Killed',
51-
'parallel\Runtime',
52-
'parallel\Runtime\Bootstrap',
53-
'parallel\Runtime\Error',
54-
'parallel\Runtime\Error\Bootstrap',
55-
'parallel\Runtime\Error\Closed',
56-
'parallel\Runtime\Error\IllegalFunction',
57-
'parallel\Runtime\Error\IllegalInstruction',
58-
'parallel\Runtime\Error\IllegalParameter',
59-
'parallel\Runtime\Error\IllegalReturn',
33+
'parallel\Channel' => 0,
34+
'parallel\Channel\Error' => 0,
35+
'parallel\Channel\Error\Closed' => 0,
36+
'parallel\Channel\Error\Existence' => 0,
37+
'parallel\Channel\Error\IllegalValue' => 0,
38+
'parallel\Error' => 0,
39+
'parallel\Events' => 0,
40+
'parallel\Events\Error' => 0,
41+
'parallel\Events\Error\Existence' => 0,
42+
'parallel\Events\Error\Timeout' => 0,
43+
'parallel\Events\Event' => 0,
44+
'parallel\Events\Event\Type' => 0,
45+
'parallel\Events\Input' => 0,
46+
'parallel\Events\Input\Error' => 0,
47+
'parallel\Events\Input\Error\Existence' => 0,
48+
'parallel\Events\Input\Error\IllegalValue' => 0,
49+
'parallel\Future' => 0,
50+
'parallel\Future\Error' => 0,
51+
'parallel\Future\Error\Cancelled' => 0,
52+
'parallel\Future\Error\Foreign' => 0,
53+
'parallel\Future\Error\Killed' => 0,
54+
'parallel\Runtime' => 0,
55+
'parallel\Runtime\Bootstrap' => 0,
56+
'parallel\Runtime\Error' => 0,
57+
'parallel\Runtime\Error\Bootstrap' => 0,
58+
'parallel\Runtime\Error\Closed' => 0,
59+
'parallel\Runtime\Error\IllegalFunction' => 0,
60+
'parallel\Runtime\Error\IllegalInstruction' => 0,
61+
'parallel\Runtime\Error\IllegalParameter' => 0,
62+
'parallel\Runtime\Error\IllegalReturn' => 0,
6063
];
6164

6265
private const MISSING_FUNCTIONS = [];
6366

6467
private const MISSING_CONSTANTS = [
65-
'STDIN',
66-
'STDOUT',
67-
'STDERR',
68+
'STDIN' => 0,
69+
'STDOUT' => 0,
70+
'STDERR' => 0,
6871
// Added in PHP 7.4
69-
'T_BAD_CHARACTER',
70-
'T_FN',
71-
'T_COALESCE_EQUAL',
72+
'T_BAD_CHARACTER' => 70400,
73+
'T_FN' => 70400,
74+
'T_COALESCE_EQUAL' => 70400,
7275
// Added in PHP 8.0
73-
'T_NAME_QUALIFIED',
74-
'T_NAME_FULLY_QUALIFIED',
75-
'T_NAME_RELATIVE',
76-
'T_MATCH',
77-
'T_NULLSAFE_OBJECT_OPERATOR',
78-
'T_ATTRIBUTE',
76+
'T_NAME_QUALIFIED' => 80000,
77+
'T_NAME_FULLY_QUALIFIED' => 80000,
78+
'T_NAME_RELATIVE' => 80000,
79+
'T_MATCH' => 80000,
80+
'T_NULLSAFE_OBJECT_OPERATOR' => 80000,
81+
'T_ATTRIBUTE' => 80000,
7982
];
8083

8184
private static $CLASSES;
@@ -87,18 +90,30 @@ final class Reflector
8790
/**
8891
* @param array<string,string>|null $symbols
8992
* @param array<string,string> $source
90-
* @param string[] $missingSymbols
93+
* @param array<string, int> $missingSymbols
9194
*/
9295
private static function initSymbolList(?array &$symbols, array $source, array $missingSymbols): void
9396
{
9497
if (null !== $symbols) {
9598
return;
9699
}
97100

101+
$excludingSymbols = array_keys(
102+
array_filter(
103+
$missingSymbols,
104+
static function ($version) {
105+
return PHP_VERSION_ID < $version;
106+
}
107+
)
108+
);
109+
98110
$symbols = array_fill_keys(
99-
array_merge(
100-
array_keys($source),
101-
$missingSymbols
111+
array_diff(
112+
array_merge(
113+
array_keys($source),
114+
array_keys($missingSymbols)
115+
),
116+
$excludingSymbols
102117
),
103118
true
104119
);

tests/ReflectorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use Generator;
1818
use PHPUnit\Framework\TestCase;
19+
use const PHP_VERSION_ID;
1920

2021
/**
2122
* @covers \Humbug\PhpScoper\Reflector
@@ -162,5 +163,10 @@ public function provideConstants(): Generator
162163
'FTP_ASCII',
163164
true,
164165
];
166+
167+
yield 'T_MATCH constant' => [
168+
'T_MATCH',
169+
PHP_VERSION_ID >= 80000,
170+
];
165171
}
166172
}

tests/Scoper/PhpScoperSpecTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@
4949
use function strpos;
5050
use function usort;
5151
use const PHP_EOL;
52+
use const PHP_VERSION_ID;
5253

5354
class PhpScoperSpecTest extends TestCase
5455
{
5556
private const SPECS_PATH = __DIR__.'/../../specs';
5657
private const SECONDARY_SPECS_PATH = __DIR__.'/../../_specs';
5758

5859
private const SPECS_META_KEYS = [
60+
'minPhpVersion',
5961
'title',
6062
'prefix',
6163
'whitelist',
@@ -99,8 +101,13 @@ public function test_can_scope_valid_files(
99101
Whitelist $whitelist,
100102
?string $expected,
101103
array $expectedRegisteredClasses,
102-
array $expectedRegisteredFunctions
104+
array $expectedRegisteredFunctions,
105+
?int $minPhpVersion
103106
): void {
107+
if (null !== $minPhpVersion && $minPhpVersion > PHP_VERSION_ID) {
108+
$this->markTestSkipped(sprintf('Min PHP version not matched for spec %s', $spec));
109+
}
110+
104111
$filePath = 'file.php';
105112
$patchers = [create_fake_patcher()];
106113
$scoper = $this->createScoper();
@@ -291,6 +298,7 @@ private function parseSpecFile(string $file, array $meta, $fixtureTitle, $fixtur
291298
'' === $payloadParts[1] ? null : $payloadParts[1], // Expected output; null means an exception is expected,
292299
$fixtureSet['registered-classes'] ?? $meta['registered-classes'],
293300
$fixtureSet['registered-functions'] ?? $meta['registered-functions'],
301+
$meta['minPhpVersion'] ?? null,
294302
];
295303
}
296304

0 commit comments

Comments
 (0)