Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
85.71% |
18 / 21 |
|
75.00% |
9 / 12 |
CRAP | |
0.00% |
0 / 1 |
JsonSchemaReader | |
85.71% |
18 / 21 |
|
75.00% |
9 / 12 |
18.94 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
4 | |||
getReflectionClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getReflectionSchemaSource | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isSchema | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
assertIsSchema | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getConstantValue | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getJsonSchemaVersion | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getVersion | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getNextVersion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getPreviousVersion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSchemaId | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getSchemaConverterId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\CommunityConfiguration\Schema; |
4 | |
5 | use InvalidArgumentException; |
6 | use MediaWiki\Settings\Source\ReflectionSchemaSource; |
7 | use ReflectionClass; |
8 | |
9 | class JsonSchemaReader implements SchemaReader { |
10 | |
11 | private ReflectionClass $class; |
12 | |
13 | /** |
14 | * @param JsonSchema|string $classNameOrClassInstance JsonSchema derived class name (instance only allowed in tests) |
15 | */ |
16 | public function __construct( $classNameOrClassInstance ) { |
17 | // @codeCoverageIgnoreStart |
18 | if ( is_object( $classNameOrClassInstance ) ) { |
19 | if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
20 | throw new InvalidArgumentException( |
21 | 'JsonSchema should never be instantiated in production code' |
22 | ); |
23 | } |
24 | if ( !( $classNameOrClassInstance instanceof JsonSchema ) ) { |
25 | throw new InvalidArgumentException( |
26 | get_class( $classNameOrClassInstance ) . ' must be instance of ' . JsonSchema::class |
27 | ); |
28 | } |
29 | } |
30 | // @codeCoverageIgnoreEnd |
31 | |
32 | $this->class = new ReflectionClass( $classNameOrClassInstance ); |
33 | } |
34 | |
35 | public function getReflectionClass(): ReflectionClass { |
36 | return $this->class; |
37 | } |
38 | |
39 | public function getReflectionSchemaSource(): ReflectionSchemaSource { |
40 | return new ReflectionSchemaSource( $this->class->getName() ); |
41 | } |
42 | |
43 | public function isSchema(): bool { |
44 | return $this->class->isSubclassOf( JsonSchema::class ); |
45 | } |
46 | |
47 | public function assertIsSchema(): void { |
48 | if ( !$this->isSchema() ) { |
49 | throw new InvalidArgumentException( |
50 | __CLASS__ . ' needs to be used with a class that implements ' |
51 | . JsonSchema::class . '.' |
52 | ); |
53 | } |
54 | } |
55 | |
56 | /** |
57 | * @param string $constantName |
58 | * @param mixed $default |
59 | * @return mixed |
60 | */ |
61 | private function getConstantValue( string $constantName, $default ) { |
62 | $this->assertIsSchema(); |
63 | $const = $this->class->getReflectionConstant( $constantName ); |
64 | return $const ? $const->getValue() : $default; |
65 | } |
66 | |
67 | public function getJsonSchemaVersion(): string { |
68 | return $this->getConstantValue( 'JSON_SCHEMA_VERSION', JsonSchema::JSON_SCHEMA_VERSION ); |
69 | } |
70 | |
71 | public function getVersion(): ?string { |
72 | return $this->getConstantValue( 'VERSION', JsonSchema::VERSION ); |
73 | } |
74 | |
75 | public function getNextVersion(): ?string { |
76 | return $this->getConstantValue( 'SCHEMA_NEXT_VERSION', JsonSchema::SCHEMA_NEXT_VERSION ); |
77 | } |
78 | |
79 | public function getPreviousVersion(): ?string { |
80 | return $this->getConstantValue( 'SCHEMA_PREVIOUS_VERSION', JsonSchema::SCHEMA_PREVIOUS_VERSION ); |
81 | } |
82 | |
83 | public function getSchemaId(): string { |
84 | $schemaId = str_replace( '\\', '/', $this->class->getName() ); |
85 | if ( $this->getVersion() ) { |
86 | $schemaId .= '/' . $this->getVersion(); |
87 | } |
88 | return $schemaId; |
89 | } |
90 | |
91 | public function getSchemaConverterId(): ?string { |
92 | return $this->getConstantValue( 'SCHEMA_CONVERTER', JsonSchema::SCHEMA_CONVERTER ); |
93 | } |
94 | } |