Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
69.81% covered (warning)
69.81%
37 / 53
83.33% covered (warning)
83.33%
10 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
Fun1
69.81% covered (warning)
69.81%
37 / 53
83.33% covered (warning)
83.33%
10 / 12
35.32
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
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%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 createMover
0.00% covered (danger)
0.00%
0 / 12
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\MMLmo;
10use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmover;
11use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmpadded;
12use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmrow;
13use MediaWiki\Extension\Math\WikiTexVC\TexUtil;
14
15class Fun1 extends TexNode {
16
17    /** @var string */
18    protected $fname;
19    /** @var TexNode */
20    protected $arg;
21
22    public function __construct( string $fname, TexNode $arg ) {
23        parent::__construct( $fname, $arg );
24        $this->fname = $fname;
25        $this->arg = $arg;
26    }
27
28    /**
29     * @return string
30     */
31    public function getFname(): string {
32        return $this->fname;
33    }
34
35    /**
36     * @return TexNode
37     */
38    public function getArg(): TexNode {
39        return $this->arg;
40    }
41
42    /** @inheritDoc */
43    public function inCurlies() {
44        return $this->render();
45    }
46
47    /** @inheritDoc */
48    public function render() {
49        return '{' . $this->fname . ' ' . $this->arg->inCurlies() . '}';
50    }
51
52    /** @inheritDoc */
53    public function toMMLTree( array $arguments = [], array &$state = [] ) {
54        $cb = TexUtil::getInstance()->callback( trim( $this->fname ) );
55        if ( is_string( $cb ) && preg_match( '#^' .
56                preg_quote( self::class ) .
57                '::(?<method>\\w+)$#', $cb, $m ) ) {
58            return $this->{$m['method']}( $arguments, $state );
59        }
60
61        return $this->parseToMML( $this->fname, $arguments, null );
62    }
63
64    public function createMover( string $inner, array $moArgs = [] ): string {
65        $mrow = new MMLmrow();
66        $mo = new MMLmo( "", $moArgs );
67        $mover = new MMLmover();
68        $ret = $mrow->encapsulateRaw(
69            $mrow->encapsulateRaw(
70                $mover->encapsulateRaw(
71                    $this->args[1]->toMMLTree() .
72                    $mo->encapsulateRaw( $inner )
73                )
74            )
75        );
76        return $ret;
77    }
78
79    /** @inheritDoc */
80    public function extractIdentifiers( $args = null ) {
81        if ( $args == null ) {
82            $args = [ $this->arg ];
83        }
84        $tu = TexUtil::getInstance();
85        $letterMods = array_keys( $tu->getBaseElements()['is_letter_mod'] );
86        if ( in_array( $this->fname, $letterMods, true ) ) {
87            $ident = $this->arg->getModIdent();
88            if ( !isset( $ident[0] ) ) {
89                return parent::extractIdentifiers( $args );
90            }
91            // in difference to javascript code: taking first element of array here.
92            return [ $this->fname . '{' . $ident[0] . '}' ];
93
94        } elseif ( array_key_exists( $this->fname, $tu->getBaseElements()['ignore_identifier'] ) ) {
95            return [];
96        }
97
98        return parent::extractIdentifiers( $args );
99    }
100
101    /** @inheritDoc */
102    public function extractSubscripts() {
103        return $this->getSubs( $this->arg->extractSubscripts() );
104    }
105
106    /** @inheritDoc */
107    public function getModIdent() {
108        return $this->getSubs( $this->arg->getModIdent() );
109    }
110
111    private function getSubs( array $subs ): array {
112        $letterMods = array_keys( TexUtil::getInstance()->getBaseElements()['is_letter_mod'] );
113
114        if ( isset( $subs[0] ) && in_array( $this->fname, $letterMods, true ) ) {
115            // in difference to javascript code: taking first element of array here.
116            return [ $this->fname . '{' . $subs[0] . '}' ];
117        }
118        return [];
119    }
120
121    private function lap(): MMLmrow {
122        $name = $this->fname;
123        if ( trim( $name ) === "\\rlap" ) {
124            $args = [ "width" => "0" ];
125        } elseif ( trim( $name ) === "\\llap" ) {
126            $args = [ "width" => "0", "lspace" => "-1width" ];
127        } else {
128            throw new InvalidArgumentException(
129                "Unsupported function for lap: $name"
130            );
131        }
132        return new MMLmrow( TexClass::ORD, [],
133            new MMLmpadded( "", $args, $this->getArg()->toMMLTree() ) );
134    }
135
136}