Skip to content

Commit 2b10b85

Browse files
authored
Add support for exposing namespaces (#608)
1 parent b089da2 commit 2b10b85

File tree

7 files changed

+109
-3
lines changed

7 files changed

+109
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ a PHPUnit PHAR with isolated code, you still want the PHAR to be able to
325325
understand the `PHPUnit\Framework\TestCase` class.
326326

327327
Notes for the rework of the docs which are coming as I'm reviewing the tests:
328-
- excluded namespaces take precedence over exposing a symbol. For example, exposing the class `Acme\Foo` and excluding the namespace `Acme` will result in the class `Acme\Foo` to NOT be exposed, but excluded instead.
328+
- excluded namespaces take precedence over exposing a symbol/namespace. For example, exposing the class `Acme\Foo` and excluding the namespace `Acme` will result in the class `Acme\Foo` to NOT be exposed, but excluded instead.
329329

330330

331331
### Constants & Classes & functions from the global namespace

specs/class/abstract.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,37 @@ public abstract function b();
245245
PHP,
246246
],
247247

248+
'Declaration in an exposed namespace' => [
249+
'expose-namespaces' => ['Foo'],
250+
'expected-recorded-classes' => [
251+
['Foo\A', 'Humbug\Foo\A'],
252+
],
253+
'payload' => <<<'PHP'
254+
<?php
255+
256+
namespace Foo;
257+
258+
abstract class A {
259+
public function a() {}
260+
abstract public function b();
261+
}
262+
----
263+
<?php
264+
265+
namespace Humbug\Foo;
266+
267+
abstract class A
268+
{
269+
public function a()
270+
{
271+
}
272+
public abstract function b();
273+
}
274+
\class_alias('Humbug\\Foo\\A', 'Foo\\A', \false);
275+
276+
PHP,
277+
],
278+
248279
'Declaration of an exposed class in a namespace' => [
249280
'expose-classes' => ['Foo\A'],
250281
'expected-recorded-classes' => [

specs/const/const-declaration-with-global-exposed.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,32 @@
194194

195195
PHP,
196196
],
197+
198+
'Exposed constants declaration in an exposed namespace' => [
199+
'expose-namespaces' => ['Acme'],
200+
'payload' => <<<'PHP'
201+
<?php
202+
203+
namespace Acme;
204+
205+
const FOO_CONST = foo();
206+
define('BAR_CONST', foo());
207+
define('Acme\BAR_CONST', foo());
208+
define(FOO_CONST, foo());
209+
define(\FOO_CONST, foo());
210+
define(\Acme\BAR_CONST, foo());
211+
----
212+
<?php
213+
214+
namespace Humbug\Acme;
215+
216+
\define('Acme\\FOO_CONST', foo());
217+
\define('BAR_CONST', foo());
218+
\define('Acme\\BAR_CONST', foo());
219+
\define(FOO_CONST, foo());
220+
\define(\FOO_CONST, foo());
221+
\define(\Acme\BAR_CONST, foo());
222+
223+
PHP,
224+
],
197225
];

specs/func-declaration/namespace.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,4 +582,28 @@ function foo() : ?\Humbug\X\Y
582582

583583
PHP,
584584
],
585+
586+
'Function declaration in an exposed namespace' => [
587+
'expose-namespaces' => ['Acme'],
588+
'expected-recorded-functions' => [
589+
['Acme\foo', 'Humbug\Acme\foo'],
590+
],
591+
'payload' => <<<'PHP'
592+
<?php
593+
594+
namespace Acme;
595+
596+
function foo() {}
597+
598+
----
599+
<?php
600+
601+
namespace Humbug\Acme;
602+
603+
function foo()
604+
{
605+
}
606+
607+
PHP,
608+
],
585609
];

src/Configuration/SymbolsConfigurationFactory.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ public function createSymbolsConfiguration(array $config): SymbolsConfiguration
4848
ConfigurationKeys::EXCLUDE_NAMESPACES_KEYWORD,
4949
);
5050

51+
[
52+
$exposedNamespaceNames,
53+
$exposedNamespaceRegexes,
54+
] = $this->retrieveElements(
55+
$config,
56+
ConfigurationKeys::EXPOSE_NAMESPACES_KEYWORD,
57+
);
58+
5159
$legacyExposedElements = self::retrieveLegacyExposedElements($config);
5260

5361
[
@@ -93,7 +101,10 @@ public function createSymbolsConfiguration(array $config): SymbolsConfiguration
93101
$excludedNamespaceNames,
94102
$excludedNamespaceRegexes,
95103
),
96-
null,
104+
NamespaceRegistry::create(
105+
$exposedNamespaceNames,
106+
$exposedNamespaceRegexes,
107+
),
97108
SymbolRegistry::create(
98109
array_merge(
99110
$exposedClassNames,

src/Symbol/EnrichedReflector.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public function belongsToExcludedNamespace(string $name): bool
3232
->belongsToRegisteredNamespace($name);
3333
}
3434

35+
private function belongsToExposedNamespace(string $name): bool
36+
{
37+
return $this->symbolsConfiguration
38+
->getExposedNamespaces()
39+
->belongsToRegisteredNamespace($name);
40+
}
41+
3542
public function isFunctionInternal(string $name): bool
3643
{
3744
return $this->reflector->isFunctionInternal($name);
@@ -75,6 +82,7 @@ public function isExposedFunction(string $resolvedName): bool
7582
|| $this->symbolsConfiguration
7683
->getExposedFunctions()
7784
->matches($resolvedName)
85+
|| $this->belongsToExposedNamespace($resolvedName)
7886
);
7987
}
8088

@@ -94,6 +102,7 @@ public function isExposedClass(string $resolvedName): bool
94102
|| $this->symbolsConfiguration
95103
->getExposedClasses()
96104
->matches($resolvedName)
105+
|| $this->belongsToExposedNamespace($resolvedName)
97106
);
98107
}
99108

@@ -117,6 +126,7 @@ public function isExposedConstant(string $name): bool
117126
|| $this->symbolsConfiguration
118127
->getExposedConstants()
119128
->matches($name)
129+
|| $this->belongsToExposedNamespace($name)
120130
);
121131
}
122132

tests/Configuration/ConfigurationFactoryTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ public function test_it_can_create_a_complete_configuration(): void
151151
'Bar',
152152
],
153153
),
154-
null,
154+
NamespaceRegistry::create(
155+
['PHPUnit\Runner'],
156+
),
155157
SymbolRegistry::create(['Foo']),
156158
SymbolRegistry::create(['Foo']),
157159
SymbolRegistry::createForConstants(['Foo']),

0 commit comments

Comments
 (0)