Skip to content

Commit

Permalink
fix internal constants
Browse files Browse the repository at this point in the history
  • Loading branch information
smoench committed Nov 4, 2020
1 parent e4a8d13 commit 25f6e56
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 52 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
runs-on: ubuntu-latest
name: Build and test
strategy:
fail-fast: false
matrix:
php: [ "7.2", "7.4" ]
composer-version: [ "1" ]
Expand Down
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions specs/exp/coalescing-assignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

return [
'meta' => [
'minPhpVersion' => 70400,
'title' => 'Null coalescing assignment operator',
// Default values. If not specified will be the one used
'prefix' => 'Humbug',
Expand Down
1 change: 1 addition & 0 deletions specs/func-arrow/global-scope-arrow-func.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

return [
'meta' => [
'minPhpVersion' => 70400,
'title' => 'Arrow function in the global namespace',
// Default values. If not specified will be the one used
'prefix' => 'Humbug',
Expand Down
1 change: 1 addition & 0 deletions specs/func-arrow/namespace-arrow-func-with-use-stmt.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

return [
'meta' => [
'minPhpVersion' => 70400,
'title' => 'Arrow function in a namespace with use statements',
// Default values. If not specified will be the one used
'prefix' => 'Humbug',
Expand Down
1 change: 1 addition & 0 deletions specs/func-arrow/namespace-arrow-func.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

return [
'meta' => [
'minPhpVersion' => 70400,
'title' => 'Arrow function in a namespace',
// Default values. If not specified will be the one used
'prefix' => 'Humbug',
Expand Down
57 changes: 57 additions & 0 deletions specs/misc/match.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
* Pádraic Brady <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

return [
'meta' => [
'title' => 'Match',
// Default values. If not specified will be the one used
'prefix' => 'Humbug',
'whitelist' => [],
'whitelist-global-constants' => false,
'whitelist-global-classes' => false,
'whitelist-global-functions' => false,
'registered-classes' => [],
'registered-functions' => [],
],
'match' => <<<'PHP'
<?php declare(strict_types=1);
namespace Acme {
use Acme\Foo\Match;
class Foo implements Match {}
}
namespace Acme\Foo {
interface Match {}
}
----
<?php
declare (strict_types=1);
namespace Humbug\Acme;
use Humbug\Acme\Foo\Match;
class Foo implements \Humbug\Acme\Foo\Match
{
}
namespace Humbug\Acme\Foo;
interface Match
{
}

PHP
];
3 changes: 2 additions & 1 deletion src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Humbug\PhpScoper\Scoper\PatchScoper;
use Humbug\PhpScoper\Scoper\PhpScoper;
use Humbug\PhpScoper\Scoper\SymfonyScoper;
use PhpParser\Lexer;
use PhpParser\Parser;
use PhpParser\ParserFactory;

Expand Down Expand Up @@ -54,7 +55,7 @@ public function getScoper(): Scoper
public function getParser(): Parser
{
if (null === $this->parser) {
$this->parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
$this->parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7, new Lexer());
}

return $this->parser;
Expand Down
107 changes: 61 additions & 46 deletions src/Reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@

namespace Humbug\PhpScoper;

use function array_diff;
use function array_fill_keys;
use function array_filter;
use function array_keys;
use function array_merge;
use JetBrains\PHPStormStub\PhpStormStubsMap;
use const PHP_VERSION_ID;
use function strtolower;

/**
Expand All @@ -27,55 +30,55 @@ final class Reflector
{
private const MISSING_CLASSES = [
// https://github.com/JetBrains/phpstorm-stubs/pull/594
'parallel\Channel',
'parallel\Channel\Error',
'parallel\Channel\Error\Closed',
'parallel\Channel\Error\Existence',
'parallel\Channel\Error\IllegalValue',
'parallel\Error',
'parallel\Events',
'parallel\Events\Error',
'parallel\Events\Error\Existence',
'parallel\Events\Error\Timeout',
'parallel\Events\Event',
'parallel\Events\Event\Type',
'parallel\Events\Input',
'parallel\Events\Input\Error',
'parallel\Events\Input\Error\Existence',
'parallel\Events\Input\Error\IllegalValue',
'parallel\Future',
'parallel\Future\Error',
'parallel\Future\Error\Cancelled',
'parallel\Future\Error\Foreign',
'parallel\Future\Error\Killed',
'parallel\Runtime',
'parallel\Runtime\Bootstrap',
'parallel\Runtime\Error',
'parallel\Runtime\Error\Bootstrap',
'parallel\Runtime\Error\Closed',
'parallel\Runtime\Error\IllegalFunction',
'parallel\Runtime\Error\IllegalInstruction',
'parallel\Runtime\Error\IllegalParameter',
'parallel\Runtime\Error\IllegalReturn',
'parallel\Channel' => 0,
'parallel\Channel\Error' => 0,
'parallel\Channel\Error\Closed' => 0,
'parallel\Channel\Error\Existence' => 0,
'parallel\Channel\Error\IllegalValue' => 0,
'parallel\Error' => 0,
'parallel\Events' => 0,
'parallel\Events\Error' => 0,
'parallel\Events\Error\Existence' => 0,
'parallel\Events\Error\Timeout' => 0,
'parallel\Events\Event' => 0,
'parallel\Events\Event\Type' => 0,
'parallel\Events\Input' => 0,
'parallel\Events\Input\Error' => 0,
'parallel\Events\Input\Error\Existence' => 0,
'parallel\Events\Input\Error\IllegalValue' => 0,
'parallel\Future' => 0,
'parallel\Future\Error' => 0,
'parallel\Future\Error\Cancelled' => 0,
'parallel\Future\Error\Foreign' => 0,
'parallel\Future\Error\Killed' => 0,
'parallel\Runtime' => 0,
'parallel\Runtime\Bootstrap' => 0,
'parallel\Runtime\Error' => 0,
'parallel\Runtime\Error\Bootstrap' => 0,
'parallel\Runtime\Error\Closed' => 0,
'parallel\Runtime\Error\IllegalFunction' => 0,
'parallel\Runtime\Error\IllegalInstruction' => 0,
'parallel\Runtime\Error\IllegalParameter' => 0,
'parallel\Runtime\Error\IllegalReturn' => 0,
];

private const MISSING_FUNCTIONS = [];

private const MISSING_CONSTANTS = [
'STDIN',
'STDOUT',
'STDERR',
'STDIN' => 0,
'STDOUT' => 0,
'STDERR' => 0,
// Added in PHP 7.4
'T_BAD_CHARACTER',
'T_FN',
'T_COALESCE_EQUAL',
'T_BAD_CHARACTER' => 70400,
'T_FN' => 70400,
'T_COALESCE_EQUAL' => 70400,
// Added in PHP 8.0
'T_NAME_QUALIFIED',
'T_NAME_FULLY_QUALIFIED',
'T_NAME_RELATIVE',
'T_MATCH',
'T_NULLSAFE_OBJECT_OPERATOR',
'T_ATTRIBUTE',
'T_NAME_QUALIFIED' => 80000,
'T_NAME_FULLY_QUALIFIED' => 80000,
'T_NAME_RELATIVE' => 80000,
'T_MATCH' => 80000,
'T_NULLSAFE_OBJECT_OPERATOR' => 80000,
'T_ATTRIBUTE' => 80000,
];

private static $CLASSES;
Expand All @@ -87,18 +90,30 @@ final class Reflector
/**
* @param array<string,string>|null $symbols
* @param array<string,string> $source
* @param string[] $missingSymbols
* @param array<string, int> $missingSymbols
*/
private static function initSymbolList(?array &$symbols, array $source, array $missingSymbols): void
{
if (null !== $symbols) {
return;
}

$excludingSymbols = array_keys(
array_filter(
$missingSymbols,
static function ($version) {
return PHP_VERSION_ID < $version;
}
)
);

$symbols = array_fill_keys(
array_merge(
array_keys($source),
$missingSymbols
array_diff(
array_merge(
array_keys($source),
array_keys($missingSymbols)
),
$excludingSymbols
),
true
);
Expand Down
6 changes: 6 additions & 0 deletions tests/ReflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Humbug\PhpScoper;

use Generator;
use const PHP_VERSION_ID;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -162,5 +163,10 @@ public function provideConstants(): Generator
'FTP_ASCII',
true,
];

yield 'T_MATCH constant' => [
'T_MATCH',
PHP_VERSION_ID >= 80000,
];
}
}
10 changes: 9 additions & 1 deletion tests/Scoper/PhpScoperSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use function implode;
use function is_array;
use const PHP_EOL;
use const PHP_VERSION_ID;
use PhpParser\Error as PhpParserError;
use PHPUnit\Framework\TestCase;
use function sprintf;
Expand All @@ -50,6 +51,7 @@ class PhpScoperSpecTest extends TestCase
private const SECONDARY_SPECS_PATH = __DIR__.'/../../_specs';

private const SPECS_META_KEYS = [
'minPhpVersion',
'title',
'prefix',
'whitelist',
Expand Down Expand Up @@ -93,8 +95,13 @@ public function test_can_scope_valid_files(
Whitelist $whitelist,
?string $expected,
array $expectedRegisteredClasses,
array $expectedRegisteredFunctions
array $expectedRegisteredFunctions,
?int $minPhpVersion
): void {
if (null !== $minPhpVersion && $minPhpVersion > PHP_VERSION_ID) {
$this->markTestSkipped(sprintf('Min PHP version not matched for spec %s', $spec));
}

$filePath = 'file.php';
$patchers = [create_fake_patcher()];
$scoper = $this->createScoper();
Expand Down Expand Up @@ -285,6 +292,7 @@ private function parseSpecFile(string $file, array $meta, $fixtureTitle, $fixtur
'' === $payloadParts[1] ? null : $payloadParts[1], // Expected output; null means an exception is expected,
$fixtureSet['registered-classes'] ?? $meta['registered-classes'],
$fixtureSet['registered-functions'] ?? $meta['registered-functions'],
$meta['minPhpVersion'] ?? null,
];
}

Expand Down

0 comments on commit 25f6e56

Please sign in to comment.