WordList   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 20.41 %

Importance

Changes 0
Metric Value
dl 20
loc 98
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getWordsCount() 0 3 1
A getFlippedWords() 0 3 1
A getWords() 0 3 1
A __construct() 0 3 1
A flipWords() 0 3 1
A getWord() 9 9 2
A getWordIndex() 9 9 2

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
namespace KoenHoeijmakers\PaperKeyGenerator\WordLists;
4
5
use KoenHoeijmakers\PaperKeyGenerator\WordLists\Exceptions\WordListException;
6
use KoenHoeijmakers\PaperKeyGenerator\WordLists\Interfaces\WordListInterface;
7
8
abstract class WordList implements WordListInterface
9
{
10
    /**
11
     * List of words.
12
     *
13
     * @var array
14
     */
15
    protected $words = [];
16
17
    /**
18
     * Flipped list of words.
19
     *
20
     * @var array
21
     */
22
    protected $flippedWords = [];
23
24
    /**
25
     * WordList constructor.
26
     */
27
    public function __construct()
28
    {
29
        $this->flipWords();
30
    }
31
32
    /**
33
     * Get the word on the given index.
34
     *
35
     * @param int $index
36
     * @return string
37
     * @throws \KoenHoeijmakers\PaperKeyGenerator\WordLists\Exceptions\WordListException
38
     */
39 View Code Duplication
    public function getWord(int $index): string
40
    {
41
        $words = $this->getWords();
42
43
        if (!array_key_exists($index, $words)) {
44
            throw new WordListException('No word found on index "' . $index . '"');
45
        }
46
47
        return $words[$index];
48
    }
49
50
    /**
51
     * Get the array of words.
52
     *
53
     * @return array
54
     */
55
    public function getWords(): array
56
    {
57
        return $this->words;
58
    }
59
60
    /**
61
     * Get the index of the given word.
62
     *
63
     * @param string $word
64
     * @return int
65
     * @throws \KoenHoeijmakers\PaperKeyGenerator\WordLists\Exceptions\WordListException
66
     */
67 View Code Duplication
    public function getWordIndex(string $word): int
68
    {
69
        $words = $this->getFlippedWords();
70
71
        if (!array_key_exists($word, $words)) {
72
            throw new WordListException('No index found for word "' . $word . '"');
73
        }
74
75
        return $words[$word];
76
    }
77
78
    /**
79
     * Get the array of flipped words.
80
     *
81
     * @return array
82
     */
83
    public function getFlippedWords(): array
84
    {
85
        return $this->flippedWords;
86
    }
87
88
    /**
89
     * Get the count of words.
90
     *
91
     * @return int
92
     */
93
    public function getWordsCount(): int
94
    {
95
        return count($this->words);
96
    }
97
98
    /**
99
     * Flips the words for quicker lookup.
100
     *
101
     * @return void
102
     */
103
    protected function flipWords()
104
    {
105
        $this->flippedWords = array_flip($this->words);
106
    }
107
}
108