Completed
Push — master ( 3c9715...f60c04 )
by Lars
01:28
created

CollectionStringy   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 63.64%

Importance

Changes 0
Metric Value
dl 0
loc 86
ccs 14
cts 22
cp 0.6364
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 4 1
A getAll() 0 4 1
A getGenerator() 0 4 1
A toStrings() 0 12 2
A addString() 0 6 1
A addStringy() 0 6 1
A createFromStrings() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stringy;
6
7
/**
8
 * @template TKey of array-key
9
 * @template T
10
 * @extends \Arrayy\Collection\Collection<TKey,T|Stringy>
11
 */
12
class CollectionStringy extends \Arrayy\Collection\Collection
13
{
14 83
    public function getType(): string
15
    {
16 83
        return Stringy::class;
17
    }
18
19
    /**
20
     * @return Stringy[]
21
     *
22
     * @psalm-return array<array-key,Stringy>
23
     */
24
    public function getAll(): array
25
    {
26
        return parent::getAll();
27
    }
28
29
    /**
30
     * @return \Generator|Stringy[]
31
     *
32
     * @psalm-return \Generator<Stringy>
33
     */
34 82
    public function getGenerator(): \Generator
35
    {
36 82
        return parent::getGenerator();
37
    }
38
39
    /**
40
     * @return string[]
41
     */
42 2
    public function toStrings(): array
43
    {
44
        // init
45 2
        $result = [];
46
47 2
        foreach ($this->getArray() as $key => $value) {
48 2
            \assert($value instanceof Stringy);
49 2
            $result[$key] = $value->toString();
50
        }
51
52 2
        return $result;
53
    }
54
55
    /**
56
     * @param string $string
57
     *
58
     * @return $this
59
     */
60
    public function addString(string $string): self
61
    {
62
        $this->add(Stringy::create($string));
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param Stringy $stringy
69
     *
70
     * @return $this
71
     */
72
    public function addStringy(Stringy $stringy): self
73
    {
74
        $this->add($stringy);
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param string[] $strings
81
     *
82
     * @psalm-pure
83
     *
84
     * @return static
85
     */
86 2
    public static function createFromStrings($strings = []): self
87
    {
88 2
        foreach ($strings as &$string) {
89 2
            $string = Stringy::create($string);
90
        }
91
92
        /**
93
         * @psalm-suppress ImpureMethodCall -> add more psalm stuff to the collection class
94
         */
95 2
        return new static($strings);
96
    }
97
}
98