Skip to content

Commit d3f78a4

Browse files
committed
MetaLoader: preload excludes, tests
1 parent 0f687b9 commit d3f78a4

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"orisai/reflection-meta": "^1.0.0",
3737
"orisai/source-map": "^1.0.0",
3838
"orisai/utils": "^1.0.0",
39+
"symfony/filesystem": "^5.4.0|^6.0.0",
3940
"symfony/polyfill-php80": "^1.27.0"
4041
},
4142
"require-dev": {

docs/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,17 @@ $metaLoader->preloadFromPaths([
18581858
]);
18591859
```
18601860

1861+
You may as well exclude paths
1862+
1863+
```php
1864+
$metaLoader->preloadFromPaths([
1865+
__DIR__ . '/path1',
1866+
__DIR__ . '/path2',
1867+
], [
1868+
__DIR__ . '/path1/excluded',
1869+
]);
1870+
```
1871+
18611872
## Tracking input values
18621873

18631874
Track input values of every mapped object in hierarchy, before they were processed by rules.

src/Meta/MetaLoader.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
use Orisai\SourceMap\ClassSource;
1414
use ReflectionClass;
1515
use ReflectionEnum;
16+
use Symfony\Component\Filesystem\Path;
1617
use function array_merge;
1718
use function array_unique;
1819
use function array_values;
1920
use function assert;
2021
use function class_exists;
22+
use function interface_exists;
2123
use function is_subclass_of;
24+
use function trait_exists;
2225

2326
final class MetaLoader
2427
{
@@ -147,18 +150,30 @@ private function createRuntimeMeta(ReflectionClass $class): array
147150

148151
/**
149152
* @param list<string> $paths
153+
* @param list<string> $excludePaths
150154
*/
151-
public function preloadFromPaths(array $paths): void
155+
public function preloadFromPaths(array $paths, array $excludePaths = []): void
152156
{
153157
$loader = new RobotLoader();
158+
$loader->reportParseErrors(false);
159+
154160
foreach ($paths as $path) {
155161
$loader->addDirectory($path);
156162
}
157163

158164
$loader->rebuild();
159165

160166
foreach ($loader->getIndexedClasses() as $class => $file) {
161-
assert(class_exists($class));
167+
foreach ($excludePaths as $excludePath) {
168+
if (Path::isBasePath($excludePath, $file)) {
169+
continue 2;
170+
}
171+
}
172+
173+
require_once $file;
174+
175+
assert(class_exists($class) || interface_exists($class) || trait_exists($class));
176+
162177
$classRef = new ReflectionClass($class);
163178

164179
if (!$classRef->isSubclassOf(MappedObject::class)) {

tests/Unit/Meta/MetaLoaderTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Tests\Orisai\ObjectMapper\Doubles\FieldNames\FieldNameIdenticalWithAnotherPropertyNameVO;
99
use Tests\Orisai\ObjectMapper\Doubles\FieldNames\MultipleIdenticalFieldNamesVO;
1010
use Tests\Orisai\ObjectMapper\Toolkit\ProcessingTestCase;
11+
use const PHP_VERSION_ID;
1112

1213
final class MetaLoaderTest extends ProcessingTestCase
1314
{
@@ -69,4 +70,42 @@ public function testMultipleIdenticalPropertyNames(): void
6970
$this->metaLoader->load(ChildCollidingFieldVO::class);
7071
}
7172

73+
/**
74+
* @runInSeparateProcess
75+
*/
76+
public function testPreload(): void
77+
{
78+
$excludes = [];
79+
$excludes[] = __DIR__ . '/../../Doubles/FieldNames/FieldNameIdenticalWithAnotherPropertyNameVO.php';
80+
$excludes[] = __DIR__ . '/../../Doubles/FieldNames/MultipleIdenticalFieldNamesVO.php';
81+
$excludes[] = __DIR__ . '/../../Doubles/FieldNames/ChildCollidingFieldVO.php';
82+
$excludes[] = __DIR__ . '/../../Doubles/Constructing/DependentVO.php';
83+
84+
if (PHP_VERSION_ID < 8_00_00) {
85+
$excludes[] = __DIR__ . '/../../Doubles/PhpVersionSpecific/AttributesVO.php';
86+
}
87+
88+
if (PHP_VERSION_ID < 8_01_00) {
89+
$excludes[] = __DIR__ . '/../../Doubles/Callbacks/ObjectInitializingVoPhp81.php';
90+
$excludes[] = __DIR__ . '/../../Doubles/Enums';
91+
$excludes[] = __DIR__ . '/../../Doubles/PhpVersionSpecific/ConstructorPromotedVO.php';
92+
$excludes[] = __DIR__ . '/../../Doubles/PhpVersionSpecific/ObjectDefaultVO.php';
93+
$excludes[] = __DIR__ . '/../../Doubles/PhpVersionSpecific/ReadonlyPropertiesVO.php';
94+
}
95+
96+
if (PHP_VERSION_ID < 8_02_00) {
97+
$excludes[] = __DIR__ . '/../../Doubles/PhpVersionSpecific/ReadonlyClassVO.php';
98+
}
99+
100+
$this->metaLoader->preloadFromPaths(
101+
[
102+
__DIR__ . '/../../Doubles',
103+
],
104+
$excludes,
105+
);
106+
107+
// Makes PHPUnit happy
108+
self::assertTrue(true);
109+
}
110+
72111
}

tools/phpstan.baseline.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ parameters:
2020
count: 1
2121
path: ../tests/Doubles/Inheritance/trait-callback.php
2222

23+
-
24+
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertTrue\\(\\) with true will always evaluate to true\\.$#"
25+
count: 1
26+
path: ../tests/Unit/Meta/MetaLoaderTest.php
27+
2328
-
2429
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertSame\\(\\) with ReflectionClass\\<Tests\\\\Orisai\\\\ObjectMapper\\\\Unit\\\\Meta\\\\Runtime\\\\CallbackRuntimeMetaTest\\> and ReflectionClass\\<Orisai\\\\ObjectMapper\\\\MappedObject\\> will always evaluate to false\\.$#"
2530
count: 1

0 commit comments

Comments
 (0)