Completed
Push — 4.0 ( ca6fdf )
by Hannes
50:39 queued 49:27
created

Pages::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace iio\libmergepdf;
6
7
/**
8
 * Parse page numbers from string
9
 */
10
final class Pages implements PagesInterface
11
{
12
    /**
13
     * @var int[] Added integer page numbers
14
     */
15
    private $pages = [];
16
17
    /**
18
     * Parse page numbers from expression string
19
     *
20
     * Pages should be formatted as 1,3,6 or 12-16 or combined. Note that pages
21
     * are merged in the order that you provide them. If you put pages 12-14
22
     * before 1-5 then 12-14 will be placed first.
23
     */
24
    public function __construct(string $expressionString = '')
25
    {
26
        $expressions = explode(
27
            ',',
28
            str_replace(' ', '', $expressionString)
29
        );
30
31
        foreach ($expressions as $expr) {
32
            if (empty($expr)) {
33
                continue;
34
            }
35
            if (ctype_digit($expr)) {
36
                $this->addPage((int)$expr);
37
                continue;
38
            }
39
            if (preg_match("/^(\d+)-(\d+)/", $expr, $matches)) {
40
                $this->addRange((int)$matches[1], (int)$matches[2]);
41
                continue;
42
            }
43
            throw new Exception("Invalid page number(s) for expression '$expr'");
44
        }
45
    }
46
47
    /**
48
     * Add a single page
49
     */
50
    public function addPage(int $page): void
51
    {
52
        $this->pages[] = $page;
53
    }
54
55
    /**
56
     * Add a range of pages
57
     */
58
    public function addRange(int $start, int $end): void
59
    {
60
        $this->pages = array_merge($this->pages, range($start, $end));
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->pages, range($start, $end)) of type array is incompatible with the declared type array<integer,integer> of property $pages.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
    }
62
63
    /**
64
     * Get iterator of page numbers
65
     */
66
    public function getIterator(): iterable
67
    {
68
        return new \ArrayIterator($this->pages);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \ArrayIterator($this->pages); (ArrayIterator) is incompatible with the return type declared by the interface iio\libmergepdf\PagesInterface::getIterator of type iio\libmergepdf\iterable.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
69
    }
70
71
    /**
72
     * Check if pages has been specified
73
     */
74
    public function isEmpty(): bool
75
    {
76
        return !empty($this->pages);
77
    }
78
}
79