Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.74% covered (warning)
76.74%
33 / 43
83.33% covered (warning)
83.33%
10 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
Fun1
76.74% covered (warning)
76.74%
33 / 43
83.33% covered (warning)
83.33%
10 / 12
26.55
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFname
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getArg
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 inCurlies
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toMMLTree
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 createMover
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 extractIdentifiers
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 extractSubscripts
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getModIdent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSubs
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 lap
60.00% covered (warning)
60.00%
6 / 10
0.00% covered (danger)
0.00%
0 / 1
3.58
1<?php
2
3declare( strict_types = 1 );
4
5namespace MediaWiki\Extension\Math\WikiTexVC\Nodes;
6
7use InvalidArgumentException;
8use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\TexConstants\TexClass;
9use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLbase;
10use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmo;
11use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmover;
12use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmpadded;
13use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmrow;
14use MediaWiki\Extension\Math\WikiTexVC\TexUtil;
15
16class Fun1 extends TexNode {
17
18    public function __construct(
19        protected readonly string $fname,
20        protected readonly TexNode $arg,
21    ) {
22        parent::__construct( $fname, $arg );
23    }
24
25    public function getFname(): string {
26        return $this->fname;
27    }
28
29    public function getArg(): TexNode {
30        return $this->arg;
31    }
32
33    /** @inheritDoc */
34    public function inCurlies() {
35        return $this->render();
36    }
37
38    /** @inheritDoc */
39    public function render() {
40        return '{' . $this->fname . ' ' . $this->arg->inCurlies() . '}';
41    }
42
43    /** @inheritDoc */
44    public function toMMLTree( array $arguments = [], array &$state = [] ): MMLbase {
45        $cb = $this->getLocalCallback( trim( $this->fname ), $arguments, [], $state );
46        if ( !$cb->isEmpty() ) {
47            return $cb;
48        }
49        return $this->parseToMML( $this->fname, $arguments, null );
50    }
51
52    public function createMover( string $inner, array $moArgs = [] ): MMLbase {
53        return new MMLmrow( TexClass::ORD, [],
54            new MMLmrow( TexClass::ORD, [],
55                ( new MMLmover() )::newSubtree( $this->args[1]->toMMLTree(),
56                    new MMLmo( "", $moArgs, $inner ) )
57            )
58        );
59    }
60
61    /** @inheritDoc */
62    public function extractIdentifiers( $args = null ) {
63        if ( $args == null ) {
64            $args = [ $this->arg ];
65        }
66        $tu = TexUtil::getInstance();
67        $letterMods = array_keys( $tu->getBaseElements()['is_letter_mod'] );
68        if ( in_array( $this->fname, $letterMods, true ) ) {
69            $ident = $this->arg->getModIdent();
70            if ( !isset( $ident[0] ) ) {
71                return parent::extractIdentifiers( $args );
72            }
73            // in difference to javascript code: taking first element of array here.
74            return [ $this->fname . '{' . $ident[0] . '}' ];
75
76        } elseif ( array_key_exists( $this->fname, $tu->getBaseElements()['ignore_identifier'] ) ) {
77            return [];
78        }
79
80        return parent::extractIdentifiers( $args );
81    }
82
83    /** @inheritDoc */
84    public function extractSubscripts() {
85        return $this->getSubs( $this->arg->extractSubscripts() );
86    }
87
88    /** @inheritDoc */
89    public function getModIdent() {
90        return $this->getSubs( $this->arg->getModIdent() );
91    }
92
93    private function getSubs( array $subs ): array {
94        $letterMods = array_keys( TexUtil::getInstance()->getBaseElements()['is_letter_mod'] );
95
96        if ( isset( $subs[0] ) && in_array( $this->fname, $letterMods, true ) ) {
97            // in difference to javascript code: taking first element of array here.
98            return [ $this->fname . '{' . $subs[0] . '}' ];
99        }
100        return [];
101    }
102
103    protected function lap(): MMLmrow {
104        $name = $this->fname;
105        if ( trim( $name ) === "\\rlap" ) {
106            $args = [ "width" => "0" ];
107        } elseif ( trim( $name ) === "\\llap" ) {
108            $args = [ "width" => "0", "lspace" => "-1width" ];
109        } else {
110            throw new InvalidArgumentException(
111                "Unsupported function for lap: $name"
112            );
113        }
114        return new MMLmrow( TexClass::ORD, [],
115            new MMLmpadded( "", $args, $this->getArg()->toMMLTree() ) );
116    }
117
118}