AbstractParser   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 22
c 2
b 0
f 1
dl 0
loc 83
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getBooleanValue() 0 8 3
A isEmpty() 0 3 1
A getSingleValue() 0 9 2
A removeEmptyData() 0 7 3
A getDivisionValue() 0 8 2
A getValue() 0 7 2
1
<?php
2
3
namespace Jackal\ImageMerge\Metadata\Parser;
4
5
/**
6
 * Class AbstractParser
7
 * @package Jackal\ImageMerge\Metadata\Parser
8
 */
9
abstract class AbstractParser implements ParserInterface
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $data = [];
15
16
    /**
17
     * @return bool
18
     */
19
    public function isEmpty()
20
    {
21
        return !$this->data;
22
    }
23
24
    /**
25
     * @param $key
26
     * @return string|null|array
27
     */
28
    protected function getValue($key)
29
    {
30
        if (isset($this->data[$key])) {
31
            return $this->data[$key];
32
        }
33
34
        return null;
35
    }
36
37
    /**
38
     * @param $key
39
     * @return int|string
40
     */
41
    protected function getDivisionValue($key)
42
    {
43
        $value = $this->getValue($key);
44
        if (strpos($value, '/1') !== false) {
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
        if (strpos(/** @scrutinizer ignore-type */ $value, '/1') !== false) {
Loading history...
45
            return (int) str_replace('/1', '', $value);
46
        }
47
48
            return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value also could return the type array which is incompatible with the documented return type integer|string.
Loading history...
49
50
    }
51
52
    /**
53
     * @param $key
54
     * @return bool|null
55
     */
56
    protected function getBooleanValue($key)
57
    {
58
        $value = $this->getSingleValue($key);
59
        if (is_null($value)) {
60
            return $value;
61
        }
62
63
        return $value == true and strtolower($value) != 'false';
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $value of type string to the boolean true. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
64
    }
65
66
    /**
67
     * @param $data
68
     * @return string|null
69
     */
70
    protected function removeEmptyData($data)
71
    {
72
        if (is_string($data) and $data == '') {
73
            return null;
74
        }
75
76
        return $data;
77
    }
78
79
    /**
80
     * @param $key
81
     * @return string|null
82
     */
83
    protected function getSingleValue($key)
84
    {
85
        $value = $this->getValue($key);
86
        if (is_array($value)) {
87
            $value = array_shift($value);
88
        }
89
        $value = preg_replace('/[\r\n]/', "\n", $value);
90
91
        return $value;
92
    }
93
}
94