BiblePassageParser::parse()   B
last analyzed

Complexity

Conditions 11
Paths 16

Size

Total Lines 57
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 37
c 1
b 0
f 0
nc 16
nop 1
dl 0
loc 57
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace TechWilk\BibleVerseParser;
6
7
use TechWilk\BibleVerseParser\Exception\UnableToParseException;
8
9
class BiblePassageParser
10
{
11
    public function parse(string $versesString): array
12
    {
13
        $sections = explode('&', $versesString);
14
15
        $verses = [];
16
        $book = '';
17
        $chapter = '';
18
        foreach ($sections as $section) {
19
            $innerSections = explode(',', $section);
20
            foreach ($innerSections as $innerSection) {
21
                $result = preg_match(
22
                    '/^\\s*((?:[0-9]+\\s+)?[^0-9]+)?([0-9]+)?(?:\\s*[\\. \\:v]\\s*([0-9\\-]+(?:end)?))?\\s*$/',
23
                    $innerSection,
24
                    $matches
25
                );
26
                if (!$result) {
27
                    throw new UnableToParseException('Unable to parse verse');
28
                }
29
30
                if (
31
                    !array_key_exists(1, $matches)
32
                    && !array_key_exists(2, $matches)
33
                    && !array_key_exists(3, $matches)
34
                ) {
35
                    throw new UnableToParseException('Unable to parse verse');
36
                }
37
38
                $matches[1] = trim($matches[1] ?? '');
39
                if ('' !== $matches[1]) {
40
                    $book = $matches[1];
41
                    $chapter = '';
42
                }
43
44
                $matches[2] = trim($matches[2] ?? '');
45
                if ('' !== $matches[2]) {
46
                    $chapter = $matches[2];
47
                }
48
49
                $verse = '';
50
                $matches[3] = trim($matches[3] ?? '');
51
                if ('' !== $matches[3]) {
52
                    if ('' === $chapter) {
53
                        $chapter = $matches[3];
54
                    } else {
55
                        $verse = $matches[3];
56
                    }
57
                }
58
59
                $verses[] = new BiblePassage(
60
                    $book,
61
                    $chapter,
62
                    $verse
63
                );
64
            }
65
        }
66
67
        return $verses;
68
    }
69
}
70