Completed
Push — master ( d29c7d...8008cc )
by Lars
06:52
created

StopWords   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 15.15 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 89.29%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 1
dl 20
loc 132
ccs 25
cts 28
cp 0.8929
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A loadLanguageData() 8 8 2
A getData() 0 18 3
A getStopWordsFromLanguage() 12 12 3
A loadLanguageDataAll() 0 8 3
A getStopWordsAll() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\helper;
6
7
/**
8
 * Phonetic-Helper-Class
9
 *
10
 * @package voku\helper
11
 */
12
final class StopWords
13
{
14
  /**
15
   * @var array
16
   */
17
  private static $availableLanguages = array(
18
      'ar',
19
      'bg',
20
      'ca',
21
      'cz',
22
      'da',
23
      'de',
24
      'el',
25
      'en',
26
      'eo',
27
      'es',
28
      'et',
29
      'fi',
30
      'fr',
31
      'hi',
32
      'hr',
33
      'hu',
34
      'id',
35
      'it',
36
      'ka',
37
      'lt',
38
      'lv',
39
      'nl',
40
      'no',
41
      'pl',
42
      'pt',
43
      'ro',
44
      'ru',
45
      'sk',
46
      'sv',
47
      'tr',
48
      'uk',
49
      'vi'
50
  );
51
52
  /**
53
   * @var array
54
   */
55
  private $stopWords = array();
56
57
  /**
58
   * Load language-data from one language.
59
   *
60
   * @param string $language
61
   *
62
   * @throws StopWordsLanguageNotExists
63
   */
64 2 View Code Duplication
  private function loadLanguageData(string $language = 'de')
0 ignored issues
show
Duplication introduced by
This method 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...
65
  {
66 2
    if (\in_array($language, self::$availableLanguages, true) === false) {
67
      throw new StopWordsLanguageNotExists('language not supported: ' . $language);
68
    }
69
70 2
    $this->stopWords[$language] = $this->getData($language);
71 2
  }
72
73
  /**
74
   * Get data from "/data/*.php".
75
   *
76
   * @param string $file
77
   *
78
   * @return array <p>Will return an empty array on error.</p>
79
   */
80 2
  private function getData(string $file): array
81
  {
82 2
    static $RESULT_STOP_WORDS_CACHE = array();
83
84 2
    if (isset($RESULT_STOP_WORDS_CACHE[$file])) {
85
      return $RESULT_STOP_WORDS_CACHE[$file];
86
    }
87
88 2
    $file = __DIR__ . '/stopwords/' . $file . '.php';
89 2
    if (file_exists($file)) {
90
      /** @noinspection PhpIncludeInspection */
91 2
      $RESULT_STOP_WORDS_CACHE[$file] = require $file;
92
    } else {
93
      $RESULT_STOP_WORDS_CACHE[$file] = array();
94
    }
95
96 2
    return $RESULT_STOP_WORDS_CACHE[$file];
97
  }
98
99
  /**
100
   * Get the stop-words from one language.
101
   *
102
   * @param string $language
103
   *
104
   * @return array
105
   *
106
   * @throws StopWordsLanguageNotExists
107
   */
108 2 View Code Duplication
  public function getStopWordsFromLanguage(string $language = 'de'): array
0 ignored issues
show
Duplication introduced by
This method 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...
109
  {
110 2
    if (\in_array($language, self::$availableLanguages, true) === false) {
111 1
      throw new StopWordsLanguageNotExists('language not supported: ' . $language);
112
    }
113
114 1
    if (!isset($this->stopWords[$language])) {
115 1
      $this->loadLanguageData($language);
116
    }
117
118 1
    return $this->stopWords[$language];
119
  }
120
121 1
  private function loadLanguageDataAll()
122
  {
123 1
    foreach (self::$availableLanguages as $language) {
124 1
      if (!isset($this->stopWords[$language])) {
125 1
        $this->loadLanguageData($language);
126
      }
127
    }
128 1
  }
129
130
  /**
131
   * Get all stop-words from all languages.
132
   *
133
   * @return array
134
   *
135
   * @throws StopWordsLanguageNotExists
136
   */
137 1
  public function getStopWordsAll(): array
138
  {
139 1
    $this->loadLanguageDataAll();
140
141 1
    return $this->stopWords;
142
  }
143
}
144