Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.94% |
31 / 33 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ZFunctionCall | |
93.94% |
31 / 33 |
|
60.00% |
3 / 5 |
13.04 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
| getDefinition | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| isValid | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| getZValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getReturnType | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WikiLambda ZFunctionCall |
| 4 | * |
| 5 | * @file |
| 6 | * @ingroup Extensions |
| 7 | * @copyright 2020– WikiLambda team; see AUTHORS.txt |
| 8 | * @license MIT |
| 9 | */ |
| 10 | |
| 11 | namespace MediaWiki\Extension\WikiLambda\ZObjects; |
| 12 | |
| 13 | use MediaWiki\Extension\WikiLambda\Registry\ZTypeRegistry; |
| 14 | use MediaWiki\Extension\WikiLambda\WikiLambdaServices; |
| 15 | use MediaWiki\Extension\WikiLambda\ZObjectUtils; |
| 16 | |
| 17 | class ZFunctionCall extends ZObject { |
| 18 | |
| 19 | /** |
| 20 | * Construct a ZFunctionCall instance, can take a referenced or derreferenced function. |
| 21 | * |
| 22 | * @param ZObject $function |
| 23 | * @param array $args |
| 24 | */ |
| 25 | public function __construct( $function, $args = [] ) { |
| 26 | $this->data[ ZTypeRegistry::Z_FUNCTIONCALL_FUNCTION ] = $function; |
| 27 | $functionZid = ''; |
| 28 | if ( $function instanceof ZFunction ) { |
| 29 | $functionZid = $function->getIdentity() ?? ''; |
| 30 | } |
| 31 | if ( $function instanceof ZReference ) { |
| 32 | $functionZid = $function->getZValue() ?? ''; |
| 33 | } |
| 34 | foreach ( $args as $key => $value ) { |
| 35 | // Save as additional keys only if the key reference belongs to the function Zid |
| 36 | if ( ZObjectUtils::getZObjectReferenceFromKey( $key ) === $functionZid ) { |
| 37 | $this->data[ $key ] = $value; |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @inheritDoc |
| 44 | */ |
| 45 | public static function getDefinition(): array { |
| 46 | return [ |
| 47 | 'type' => [ |
| 48 | 'type' => ZTypeRegistry::Z_REFERENCE, |
| 49 | 'value' => ZTypeRegistry::Z_FUNCTIONCALL, |
| 50 | ], |
| 51 | 'keys' => [ |
| 52 | ZTypeRegistry::Z_FUNCTIONCALL_FUNCTION => [ |
| 53 | 'type' => ZTypeRegistry::Z_REFERENCE, |
| 54 | ], |
| 55 | ], |
| 56 | 'additionalKeys' => true |
| 57 | ]; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @inheritDoc |
| 62 | */ |
| 63 | public function isValid(): bool { |
| 64 | // Check Z7K1 is either a valid reference or a valid function |
| 65 | $function = $this->data[ ZTypeRegistry::Z_FUNCTIONCALL_FUNCTION ]; |
| 66 | if ( |
| 67 | !( $this->data[ ZTypeRegistry::Z_FUNCTIONCALL_FUNCTION ] instanceof ZReference ) && |
| 68 | !( $this->data[ ZTypeRegistry::Z_FUNCTIONCALL_FUNCTION ] instanceof ZFunction ) ) { |
| 69 | return false; |
| 70 | } |
| 71 | return $this->data[ ZTypeRegistry::Z_FUNCTIONCALL_FUNCTION ]->isValid(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns Function Zid |
| 76 | * |
| 77 | * @return string |
| 78 | */ |
| 79 | public function getZValue(): string { |
| 80 | return (string)( $this->data[ ZTypeRegistry::Z_FUNCTIONCALL_FUNCTION ]->getZValue() ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Resulting type of this ZFunctionCall. If the ZFunction is not available, returns null. |
| 85 | * When Z7K1 is a reference to a persisted function, its type should be extracted from the |
| 86 | * secondary labels database for better performance. |
| 87 | * |
| 88 | * @return string|null |
| 89 | */ |
| 90 | public function getReturnType() { |
| 91 | if ( $this->data[ ZTypeRegistry::Z_FUNCTIONCALL_FUNCTION ] instanceof ZReference ) { |
| 92 | $zObjectStore = WikiLambdaServices::getZObjectStore(); |
| 93 | return $zObjectStore->fetchZFunctionReturnType( $this->getZValue() ); |
| 94 | } |
| 95 | |
| 96 | if ( $this->data[ ZTypeRegistry::Z_FUNCTIONCALL_FUNCTION ] instanceof ZFunction ) { |
| 97 | return $this->data[ ZTypeRegistry::Z_FUNCTIONCALL_FUNCTION ]->getReturnType(); |
| 98 | } |
| 99 | |
| 100 | return null; |
| 101 | } |
| 102 | } |