Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
JsonSchemaReader
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
9 / 9
15
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
4
 getReflectionClass
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getReflectionSchemaSource
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isJsonSchema
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 assertIsJsonSchema
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getConstantValue
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getJsonSchemaVersion
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getVersion
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSchemaId
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace MediaWiki\Extension\CommunityConfiguration\Schema;
4
5use InvalidArgumentException;
6use MediaWiki\Settings\Source\ReflectionSchemaSource;
7use ReflectionClass;
8
9class JsonSchemaReader {
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 isJsonSchema(): bool {
44        return $this->class->isSubclassOf( JsonSchema::class );
45    }
46
47    public function assertIsJsonSchema(): void {
48        if ( !$this->isJsonSchema() ) {
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->assertIsJsonSchema();
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 getSchemaId(): string {
76        $schemaId = str_replace( '\\', '/', $this->class->getName() );
77        if ( $this->getVersion() ) {
78            $schemaId .= '/' . $this->getVersion();
79        }
80        return $schemaId;
81    }
82}