Skip to content

Commit ef5f94d

Browse files
Initial work on metadata objects for #4502
1 parent f17691d commit ef5f94d

File tree

9 files changed

+482
-0
lines changed

9 files changed

+482
-0
lines changed

src/Util/Metadata/Metadata.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Util\Metadata;
11+
12+
/**
13+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
14+
* @psalm-immutable
15+
*/
16+
abstract class Metadata
17+
{
18+
public function isAfter(): bool
19+
{
20+
return false;
21+
}
22+
23+
public function isAfterClass(): bool
24+
{
25+
return false;
26+
}
27+
28+
public function isBackupGlobals(): bool
29+
{
30+
return false;
31+
}
32+
33+
public function isBackupStaticProperties(): bool
34+
{
35+
return false;
36+
}
37+
38+
public function isBeforeClass(): bool
39+
{
40+
return false;
41+
}
42+
43+
public function isBefore(): bool
44+
{
45+
return false;
46+
}
47+
48+
public function isCodeCoverageIgnore(): bool
49+
{
50+
return false;
51+
}
52+
53+
public function isCoversNothing(): bool
54+
{
55+
return false;
56+
}
57+
58+
public function isDoesNotPerformAssertions(): bool
59+
{
60+
return false;
61+
}
62+
63+
public function isGroup(): bool
64+
{
65+
return false;
66+
}
67+
68+
public function isRunTestsInSeparateProcesses(): bool
69+
{
70+
return false;
71+
}
72+
73+
public function isRunInSeparateProcess(): bool
74+
{
75+
return false;
76+
}
77+
78+
public function isTest(): bool
79+
{
80+
return false;
81+
}
82+
83+
public function isPreCondition(): bool
84+
{
85+
return false;
86+
}
87+
88+
public function isPostCondition(): bool
89+
{
90+
return false;
91+
}
92+
93+
public function isPreserveGlobalState(): bool
94+
{
95+
return false;
96+
}
97+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Util\Metadata;
11+
12+
use function count;
13+
use Countable;
14+
use IteratorAggregate;
15+
16+
/**
17+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
18+
* @psalm-immutable
19+
*/
20+
final class MetadataCollection implements Countable, IteratorAggregate
21+
{
22+
/**
23+
* @var Metadata[]
24+
*/
25+
private $metadata;
26+
27+
/**
28+
* @param Metadata[] $metadata
29+
*/
30+
public static function fromArray(array $metadata): self
31+
{
32+
return new self(...$metadata);
33+
}
34+
35+
private function __construct(Metadata ...$metadata)
36+
{
37+
$this->metadata = $metadata;
38+
}
39+
40+
/**
41+
* @return Metadata[]
42+
*/
43+
public function asArray(): array
44+
{
45+
return $this->metadata;
46+
}
47+
48+
public function count(): int
49+
{
50+
return count($this->metadata);
51+
}
52+
53+
public function getIterator(): MetadataCollectionIterator
54+
{
55+
return new MetadataCollectionIterator($this);
56+
}
57+
58+
public function mergeWith(self $other): self
59+
{
60+
return new self(
61+
array_merge(
62+
$this->asArray(),
63+
$other->asArray()
64+
)
65+
);
66+
}
67+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Util\Metadata;
11+
12+
use function count;
13+
use function iterator_count;
14+
use Countable;
15+
use Iterator;
16+
17+
/**
18+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
19+
*/
20+
final class MetadataCollectionIterator implements Countable, Iterator
21+
{
22+
/**
23+
* @var Metadata[]
24+
*/
25+
private $metadata;
26+
27+
/**
28+
* @var int
29+
*/
30+
private $position = 0;
31+
32+
public function __construct(MetadataCollection $metadata)
33+
{
34+
$this->metadata = $metadata->asArray();
35+
}
36+
37+
public function count(): int
38+
{
39+
return iterator_count($this);
40+
}
41+
42+
public function rewind(): void
43+
{
44+
$this->position = 0;
45+
}
46+
47+
public function valid(): bool
48+
{
49+
return $this->position < count($this->metadata);
50+
}
51+
52+
public function key(): int
53+
{
54+
return $this->position;
55+
}
56+
57+
public function current(): Metadata
58+
{
59+
return $this->metadata[$this->position];
60+
}
61+
62+
public function next(): void
63+
{
64+
$this->position++;
65+
}
66+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Util\Metadata;
11+
12+
/**
13+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
14+
*/
15+
final class AnnotationReader implements Reader
16+
{
17+
/**
18+
* @psalm-param class-string $className
19+
*/
20+
public function forClass(string $className): MetadataCollection
21+
{
22+
return MetadataCollection::fromArray([]);
23+
}
24+
25+
/**
26+
* @psalm-param class-string $className
27+
*/
28+
public function forMethod(string $className, string $methodName): MetadataCollection
29+
{
30+
return MetadataCollection::fromArray([]);
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Util\Metadata;
11+
12+
/**
13+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
14+
*/
15+
final class AttributeReader implements Reader
16+
{
17+
/**
18+
* @psalm-param class-string $className
19+
*/
20+
public function forClass(string $className): MetadataCollection
21+
{
22+
return MetadataCollection::fromArray([]);
23+
}
24+
25+
/**
26+
* @psalm-param class-string $className
27+
*/
28+
public function forMethod(string $className, string $methodName): MetadataCollection
29+
{
30+
return MetadataCollection::fromArray([]);
31+
}
32+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Util\Metadata;
11+
12+
/**
13+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
14+
*/
15+
final class CachingReader implements Reader
16+
{
17+
/**
18+
* @var Reader
19+
*/
20+
private $reader;
21+
22+
/**
23+
* @var array
24+
*/
25+
private $cache = [];
26+
27+
public function __construct(Reader $reader)
28+
{
29+
$this->reader = $reader;
30+
}
31+
32+
/**
33+
* @psalm-param class-string $className
34+
*/
35+
public function forClass(string $className): MetadataCollection
36+
{
37+
if (isset($this->cache[$className])) {
38+
return $this->cache[$className];
39+
}
40+
41+
$this->cache[$className] = $this->reader->forClass($className);
42+
43+
return $this->cache[$className];
44+
}
45+
46+
/**
47+
* @psalm-param class-string $className
48+
*/
49+
public function forMethod(string $className, string $methodName): MetadataCollection
50+
{
51+
$key = $className . '::' . $methodName;
52+
53+
if (isset($this->cache[$key])) {
54+
return $this->cache[$key];
55+
}
56+
57+
$this->cache[$key] = $this->reader->forMethod($className, $methodName);
58+
59+
return $this->cache[$key];
60+
}
61+
}

0 commit comments

Comments
 (0)