ImageRule::check()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 31
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 13
cts 13
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 13
nc 6
nop 1
crap 5
1
<?php
2
3
namespace Zortje\SEO\Page;
4
5
use Zortje\Rules\Result;
6
use Zortje\Rules\ResultCollection;
7
use Zortje\Rules\RuleInterface;
8
use Zortje\Rules\Subject;
9
10
/**
11
 * Class ImageRule
12
 *
13
 * @package Zortje\SEO\Page
14
 */
15
class ImageRule implements RuleInterface
16
{
17
18
    /**
19
     * Check image rules
20
     *
21
     * @param Subject $subject
22
     *
23
     * @return ResultCollection
24
     */
25 3
    public function check(Subject $subject): ResultCollection
26
    {
27 3
        $resultCollection = new ResultCollection();
28
29
        /**
30
         * Check
31
         */
32 3
        $contextDescription = $subject->getContextDescription();
33
34 3
        $imgTags = $subject->getDom()->getElementsByTagName('img');
35
36
        /**
37
         * @var \DOMElement $imgTag
38
         */
39 3
        $missingAltAttributes = 0;
40
41 3
        foreach ($imgTags as $imgTag) {
42 3
            $altAttribute = $imgTag->attributes->getNamedItem('alt');
43
44 3
            if (!$altAttribute || empty($altAttribute->nodeValue)) {
45 3
                $missingAltAttributes++;
46
            }
47
        }
48
49 3
        if ($missingAltAttributes > 0) {
50 3
            $resultCollection->add(new Result($contextDescription,
51 3
                "$missingAltAttributes img tag is missing alt attribute"));
52
        }
53
54 3
        return $resultCollection;
55
    }
56
}
57