Statistics::getTotalSavedBytes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ArjanSchouten\HtmlMinifier\Statistics;
4
5
class Statistics implements StatisticsInterface
6
{
7
    /**
8
     * @var \ArjanSchouten\HtmlMinifier\Statistics\ReferencePoint[]
9
     */
10
    private $referencePoints;
11
12
    /**
13
     * Create statistics with the input before modification.
14
     *
15
     * @param string $input
16
     * @param string $keyName
17
     */
18 5
    public function __construct($input, $keyName = 'Original input')
19
    {
20 5
        $this->referencePoints[$keyName] = new ReferencePoint($keyName, mb_strlen($input, '8bit'));
21 5
    }
22
23
    /**
24
     * Add a step and measure the input size.
25
     *
26
     * @param int $inputSize
27
     * @param string $keyname
0 ignored issues
show
Documentation introduced by
Should the type for parameter $keyname not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
28
     *
29
     * @return \ArjanSchouten\HtmlMinifier\Statistics\ReferencePoint[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array<*,ReferencePoint>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
30
     */
31 4
    public function createReferencePoint($inputSize, $keyname = null)
32
    {
33 4
        if ($keyname === null) {
34
            $keyname = 'Step: '.(count($this->referencePoints) + 1);
35
        }
36
37 4
        if (!array_key_exists($keyname, $this->referencePoints)) {
38 4
            $this->referencePoints[$keyname] = new ReferencePoint($keyname, $inputSize);
39
        } else {
40
            $this->referencePoints[$keyname]->addBytes($inputSize);
41
        }
42
43 4
        return $this->referencePoints;
44
    }
45
46
    /**
47
     * Get all the steps which are measured.
48
     *
49
     * @return \ArjanSchouten\HtmlMinifier\Statistics\ReferencePoint[]
50
     */
51 2
    public function getReferencePoints()
52
    {
53 2
        return $this->referencePoints;
54
    }
55
56
    /**
57
     * Get the total saved bytes in bytes.
58
     *
59
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
60
     */
61 1
    public function getTotalSavedBytes()
62
    {
63 1
        $initialStep = array_first($this->referencePoints);
64 1
        $lastStep = array_last($this->referencePoints);
65
66 1
        return $initialStep->getBytes() - $lastStep->getBytes();
67
    }
68
}
69