Bible   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 94
Duplicated Lines 6.38 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 93.88%

Importance

Changes 0
Metric Value
dl 6
loc 94
ccs 46
cts 49
cp 0.9388
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A lang() 3 10 3
A version() 3 7 2
A book() 0 7 1
A chapter() 0 6 1
A verse() 0 6 1
A getBook() 0 7 2
A getChapter() 0 6 2
A getVerse() 0 8 3
A get() 0 10 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
namespace Djunehor\Logos;
4
5
class Bible
6
{
7
    private $lang;
8
    private $version;
9
    private $bible;
10
    private $book;
11
    private $chapter;
12
    private $verse;
13
14 11
    public function __construct($lang = 'en', $version = 'kjv')
15
    {
16 11
        $this->lang($lang);
17 9
        $this->version($version);
18
19 8
        $this->lang = $lang;
20 8
        $this->version = $version;
21 8
    }
22
23 11
    public function lang($lang)
24
    {
25 11
        if (! file_exists(__DIR__.'/../bibles/'.$lang)) {
26 1
            throw new \Exception("The language [$lang] doesn't exist");
27
        }
28 10 View Code Duplication
        if (! file_exists(__DIR__.'/../bibles/'.$lang.'/Books.json')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
29 1
            throw new \Exception("Index of available books not found for [$lang]");
30
        }
31 9
        $this->lang = $lang;
32 9
    }
33
34 9
    public function version($version)
35
    {
36 9 View Code Duplication
        if (! file_exists(__DIR__.'/../bibles/'.$this->lang.'/'.$version)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
37 1
            throw new \Exception("The bible version [$version] for language [$this->lang] doesn't exist");
38
        }
39 8
        $this->version = $version;
40 8
    }
41
42 6
    public function book($bookName)
43
    {
44 6
        $this->bible = json_decode(file_get_contents(__DIR__."/../bibles/$this->lang/Books.json"), true);
45 6
        $this->book = ucfirst($bookName);
46
47 6
        return $this;
48
    }
49
50 5
    public function chapter(int $chapterNumber)
51
    {
52 5
        $this->chapter = $chapterNumber - 1;
53
54 5
        return $this;
55
    }
56
57 4
    public function verse(int $verseNumber)
58
    {
59 4
        $this->verse = $verseNumber - 1;
60
61 4
        return $this;
62
    }
63
64 6
    public function getBook()
65
    {
66 6
        $bookPath = __DIR__."/../bibles/$this->lang/$this->version/$this->book.json";
67 6
        if (file_exists($bookPath)) {
68 6
            return json_decode(file_get_contents($bookPath), true);
69
        }
70
    }
71
72 5
    public function getChapter()
73
    {
74 5
        if ($book = $this->getBook()) {
75 5
            return $book['chapters'][$this->chapter] ?? null;
76
        }
77
    }
78
79 4
    public function getVerse()
80
    {
81 4
        if ($chapter = $this->getChapter()) {
82 4
            if ($verse = $chapter['verses'][$this->verse]) {
83 4
                return $verse['text'] ?? null;
84
            }
85
        }
86
    }
87
88 3
    public function get($string)
89
    {
90 3
        $explodeSpace = explode(' ', $string);
91 3
        $this->book($explodeSpace[0]);
92 3
        $explodeColumn = explode(':', $explodeSpace[1]);
93 3
        $this->chapter($explodeColumn[0]);
94 3
        $this->verse($explodeColumn[1]);
95
96 3
        return $this->getVerse();
97
    }
98
}
99