Completed
Branch releases/v0.2.2 (1aa4be)
by Luke
03:21 queued 01:01
created

CharCollection::unshift()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * CSVelte: Slender, elegant CSV for PHP
4
 *
5
 * Inspired by Python's CSV module and Frictionless Data and the W3C's CSV
6
 * standardization efforts, CSVelte was written in an effort to take all the
7
 * suck out of working with CSV.
8
 *
9
 * @version   v${CSVELTE_DEV_VERSION}
10
 * @copyright Copyright (c) 2016 Luke Visinoni <[email protected]>
11
 * @author    Luke Visinoni <[email protected]>
12
 * @license   https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT)
13
 */
14
namespace CSVelte\Collection;
15
16
class CharCollection extends AbstractCollection
17
{
18
    /**
19
     * Convert input data to an array.
20
     *
21
     * Convert the input data to an array that can be worked with by a collection.
22
     *
23
     * @param mixed $data The input data
24
     * @return array
25
     */
26 25
    protected function prepareData($data)
27
    {
28 25
        if (!is_string($data)) {
29 1
            $data = (string) $data;
30 1
        }
31 25
        return str_split($data);
32
    }
33
34
    /**
35
     * Apply a callback to each item in collection.
36
     *
37
     * Applies a callback to each item in collection and returns a new collection
38
     * containing each iteration's return value.
39
     *
40
     * @param callable $callback The callback to apply
41
     * @return AbstractCollection A new collection with callback return values
42
     */
43 2
    public function map(callable $callback)
44
    {
45 2
        return new self(implode('', array_map($callback, $this->data)));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new self(implode(...llback, $this->data))); (CSVelte\Collection\CharCollection) is incompatible with the return type of the parent method CSVelte\Collection\AbstractCollection::map of type CSVelte\Collection\Collection.

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...
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 1
    public function push(...$items)
52
    {
53 1
        $result = parent::push(...$items);
54 1
        return new self(implode('', $result->toArray()));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new self(implode('', $result->toArray())); (CSVelte\Collection\CharCollection) is incompatible with the return type of the parent method CSVelte\Collection\AbstractCollection::push of type CSVelte\Collection\Collection.

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...
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 1
    public function unshift(...$items)
61
    {
62 1
        $result = parent::unshift(...$items);
63 1
        return new self(implode('', $result->toArray()));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new self(implode('', $result->toArray())); (CSVelte\Collection\CharCollection) is incompatible with the return type of the parent method CSVelte\Collection\AbstractCollection::unshift of type CSVelte\Collection\Collection.

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...
64
    }
65
66
67
    /**
68
     * Is data consistent with this collection type?
69
     *
70
     * @param mixed $data The data to check
71
     * @return bool
72
     */
73 25
    protected function isConsistentDataStructure($data)
74
    {
75 25
        return static::isCharacterSet($data);
76
    }
77
78
}