ShiConverter::loadDefaultTables()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Shilha specific code.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
 * http://www.gnu.org/copyleft/gpl.html
19
 *
20
 * @file
21
 * @ingroup Language
22
 */
23
24
/**
25
 * Conversion script between Latin and Tifinagh for Tachelhit.
26
 * - Tifinagh -> lowercase Latin
27
 * - lowercase/uppercase Latin -> Tifinagh
28
 *
29
 *
30
 * Based on:
31
 *   - https://en.wikipedia.org/wiki/Shilha_language
32
 *   - LanguageSr.php
33
 *
34
 * @ingroup Language
35
 */
36
class ShiConverter extends LanguageConverter {
37
	protected $mDoContentConvert;
38
39
	public $mToLatin = [
40
		'ⴰ' => 'a', 'ⴱ' => 'b', 'ⴳ' => 'g', 'ⴷ' => 'd', 'ⴹ' => 'ḍ', 'ⴻ' => 'e',
41
		'ⴼ' => 'f', 'ⴽ' => 'k', 'ⵀ' => 'h', 'ⵃ' => 'ḥ', 'ⵄ' => 'ε', 'ⵅ' => 'x',
42
		'ⵇ' => 'q', 'ⵉ' => 'i', 'ⵊ' => 'j', 'ⵍ' => 'l', 'ⵎ' => 'm', 'ⵏ' => 'n',
43
		'ⵓ' => 'u', 'ⵔ' => 'r', 'ⵕ' => 'ṛ', 'ⵖ' => 'γ', 'ⵙ' => 's', 'ⵚ' => 'ṣ',
44
		'ⵛ' => 'š', 'ⵜ' => 't', 'ⵟ' => 'ṭ', 'ⵡ' => 'w', 'ⵢ' => 'y', 'ⵣ' => 'z',
45
		'ⵥ' => 'ẓ', 'ⵯ' => 'ʷ', 'ⵖ' => 'ɣ', 'ⵠ' => 'v', 'ⵒ' => 'p',
46
	];
47
48
	public $mUpperToLowerCaseLatin = [
49
		'A' => 'a', 'B' => 'b', 'C' => 'c', 'D' => 'd', 'E' => 'e',
50
		'F' => 'f', 'G' => 'g', 'H' => 'h', 'I' => 'i', 'J' => 'j',
51
		'K' => 'k', 'L' => 'l', 'M' => 'm', 'N' => 'n', 'O' => 'o',
52
		'P' => 'p', 'Q' => 'q', 'R' => 'r', 'S' => 's', 'T' => 't',
53
		'U' => 'u', 'V' => 'v', 'W' => 'w', 'X' => 'x', 'Y' => 'y',
54
		'Z' => 'z', 'Ɣ' => 'ɣ',
55
	];
56
57
	public $mToTifinagh = [
58
		'a' => 'ⴰ', 'b' => 'ⴱ', 'g' => 'ⴳ', 'd' => 'ⴷ', 'ḍ' => 'ⴹ', 'e' => 'ⴻ',
59
		'f' => 'ⴼ', 'k' => 'ⴽ', 'h' => 'ⵀ', 'ḥ' => 'ⵃ', 'ε' => 'ⵄ', 'x' => 'ⵅ',
60
		'q' => 'ⵇ', 'i' => 'ⵉ', 'j' => 'ⵊ', 'l' => 'ⵍ', 'm' => 'ⵎ', 'n' => 'ⵏ',
61
		'u' => 'ⵓ', 'r' => 'ⵔ', 'ṛ' => 'ⵕ', 'γ' => 'ⵖ', 's' => 'ⵙ', 'ṣ' => 'ⵚ',
62
		'š' => 'ⵛ', 't' => 'ⵜ', 'ṭ' => 'ⵟ', 'w' => 'ⵡ', 'y' => 'ⵢ', 'z' => 'ⵣ',
63
		'ẓ' => 'ⵥ', 'ʷ' => 'ⵯ', 'ɣ' => 'ⵖ', 'v' => 'ⵠ', 'p' => 'ⵒ',
64
	];
65
66 View Code Duplication
	function loadDefaultTables() {
67
		$this->mTables = [
68
			'lowercase' => new ReplacementArray( $this->mUpperToLowerCaseLatin ),
69
			'shi-tfng' => new ReplacementArray( $this->mToTifinagh ),
70
			'shi-latn' => new ReplacementArray( $this->mToLatin ),
71
			'shi' => new ReplacementArray()
72
		];
73
	}
74
75
	/**
76
	 * rules should be defined as -{Tifinagh | Latin-} -or-
77
	 * -{code:text | code:text | ...}-
78
	 * update: delete all rule parsing because it's not used
79
	 * currently, and just produces a couple of bugs
80
	 *
81
	 * @param string $rule
82
	 * @param array $flags
83
	 * @return array
84
	 */
85 View Code Duplication
	function parseManualRule( $rule, $flags = [] ) {
86
		if ( in_array( 'T', $flags ) ) {
87
			return parent::parseManualRule( $rule, $flags );
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class LanguageConverter as the method parseManualRule() does only exist in the following sub-classes of LanguageConverter: IuConverter, KkConverter, ShiConverter, SrConverter. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
88
		}
89
90
		$carray = [];
91
		// otherwise ignore all formatting
92
		foreach ( $this->mVariants as $v ) {
93
			$carray[$v] = $rule;
94
		}
95
96
		return $carray;
97
	}
98
99
	/**
100
	 * Do not convert content on talk pages
101
	 *
102
	 * @param string $text
103
	 * @param Parser $parser
104
	 * @return string
105
	 */
106
	function parserConvert( $text, &$parser ) {
107
		$this->mDoContentConvert = !( is_object( $parser->getTitle() )
108
			&& $parser->getTitle()->isTalkPage() );
109
110
		return parent::parserConvert( $text, $parser );
0 ignored issues
show
Bug introduced by
The method parserConvert() does not exist on LanguageConverter. Did you maybe mean convert()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
111
	}
112
113
	/**
114
	 * A function wrapper:
115
	 *   - if there is no selected variant, leave the link
116
	 *     names as they were
117
	 *   - do not try to find variants for usernames
118
	 *
119
	 * @param string &$link
120
	 * @param Title &$nt
121
	 * @param bool $ignoreOtherCond
122
	 */
123 View Code Duplication
	function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
124
		// check for user namespace
125
		if ( is_object( $nt ) ) {
126
			$ns = $nt->getNamespace();
127
			if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
128
				return;
129
			}
130
		}
131
132
		$oldlink = $link;
133
		parent::findVariantLink( $link, $nt, $ignoreOtherCond );
134
		if ( $this->getPreferredVariant() == $this->mMainLanguageCode ) {
135
			$link = $oldlink;
136
		}
137
	}
138
139
	/**
140
	 * It translates text into variant
141
	 *
142
	 * @param string $text
143
	 * @param string $toVariant
144
	 *
145
	 * @return string
146
	 */
147 View Code Duplication
	function translate( $text, $toVariant ) {
148
		// If $text is empty or only includes spaces, do nothing
149
		// Otherwise translate it
150
		if ( trim( $text ) ) {
151
			$this->loadTables();
152
			// To Tifinagh, first translate uppercase to lowercase Latin
153
			if ( $toVariant == 'shi-tfng' ) {
154
				$text = $this->mTables['lowercase']->replace( $text );
155
			}
156
			$text = $this->mTables[$toVariant]->replace( $text );
157
		}
158
		return $text;
159
	}
160
}
161
162
/**
163
 * Tachelhit
164
 *
165
 * @ingroup Language
166
 */
167 View Code Duplication
class LanguageShi extends Language {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
	function __construct() {
169
		parent::__construct();
170
171
		$variants = [ 'shi', 'shi-tfng', 'shi-latn' ];
172
		$variantfallbacks = [
173
			'shi' => 'shi-tfng',
174
			'shi-tfng' => 'shi',
175
			'shi-latn' => 'shi',
176
		];
177
178
		$flags = [];
179
		$this->mConverter = new ShiConverter( $this, 'shi', $variants, $variantfallbacks, $flags );
180
	}
181
}
182