Skip to content

Commit d8b2715

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

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
use function array_values;
1919
use function assert;
2020
use function class_exists;
21+
use function interface_exists;
2122
use function is_subclass_of;
23+
use function trait_exists;
2224

2325
final class MetaLoader
2426
{
@@ -147,18 +149,27 @@ private function createRuntimeMeta(ReflectionClass $class): array
147149

148150
/**
149151
* @param list<string> $paths
152+
* @param list<string> $excludePaths
150153
*/
151-
public function preloadFromPaths(array $paths): void
154+
public function preloadFromPaths(array $paths, array $excludePaths = []): void
152155
{
153156
$loader = new RobotLoader();
157+
154158
foreach ($paths as $path) {
155159
$loader->addDirectory($path);
156160
}
157161

162+
foreach ($excludePaths as $excludePath) {
163+
$loader->excludeDirectory($excludePath);
164+
}
165+
158166
$loader->rebuild();
159167

160168
foreach ($loader->getIndexedClasses() as $class => $file) {
161-
assert(class_exists($class));
169+
require_once $file;
170+
171+
assert(class_exists($class) || interface_exists($class) || trait_exists($class));
172+
162173
$classRef = new ReflectionClass($class);
163174

164175
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)