InteractsWithTitle   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 14
c 2
b 0
f 0
dl 0
loc 35
ccs 10
cts 12
cp 0.8333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getTitle() 0 11 4
A setTitle() 0 11 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpEpub\Traits;
6
7
use SimpleXMLElement;
8
9
trait InteractsWithTitle
10
{
11
    private readonly SimpleXMLElement $opfXml;
12
    private readonly string $dcNamespace;
13
14
    /**
15
     * Gets the title of the EPUB.
16
     */
17 9
    public function getTitle(): string
18
    {
19 9
        $this->opfXml->registerXPathNamespace('dc', $this->dcNamespace);
20
21 9
        $titleNode = $this->opfXml->xpath('//dc:title');
22
23 9
        if ($titleNode === false || $titleNode === null || $titleNode === []) {
24
            return '';
25
        }
26
27 9
        return (string) $titleNode[0];
28
    }
29
30
    /**
31
     * Sets the title of the EPUB.
32
     */
33 1
    public function setTitle(string $title): void
34
    {
35 1
        $this->opfXml->registerXPathNamespace('dc', $this->dcNamespace);
36
37 1
        $titleNode = $this->opfXml->xpath('//dc:title');
38
39 1
        if ($titleNode !== false && $titleNode !== null && $titleNode !== [] && isset($titleNode[0])) {
40
            // @phpstan-ignore-next-line
41 1
            $titleNode[0][0] = $title;
42
        } else {
43
            $this->opfXml->metadata->addChild('title', $title, $this->dcNamespace);
44
        }
45
    }
46
}
47