Completed
Push — master ( 292a08...958910 )
by Lars
01:29
created

CollectionStringy::createFromStrings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 13
ccs 5
cts 5
cp 1
crap 2
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stringy;
6
7
/**
8
 * @template TKey of array-key
9
 * @template T of Stringy
10
 * @extends \Arrayy\Collection\Collection<TKey,T>
11
 */
12
class CollectionStringy extends \Arrayy\Collection\Collection
13
{
14
    /**
15
     * Creates an CollectionInterface object.
16
     *
17
     * @param mixed  $data
18
     * @param string $iteratorClass
19
     * @param bool   $checkPropertiesInConstructor
20
     *
21
     * @return static
22
     *                <p>(Immutable) Returns an new instance of the CollectionInterface object.</p>
23
     *
24
     * @template TKeyCreate as array-key
25
     * @template TCreate of Stringy
26
     *
27
     * @phpstan-param  array<TKeyCreate,TCreate> $data
28
     * @phpstan-param  class-string<\Arrayy\ArrayyIterator<array-key, mixed>> $iteratorClass
29
     * @phpstan-return static<TKeyCreate,TCreate>
30
     *
31
     * @psalm-mutation-free
32
     */
33 79
    public static function create(
34
        $data = [],
35
        string $iteratorClass = \Arrayy\ArrayyIterator::class,
36
        bool $checkPropertiesInConstructor = true
37
    ) {
38 79
        return new static(
39 79
            $data,
40
            $iteratorClass,
41
            $checkPropertiesInConstructor
42
        );
43
    }
44
45 83
    public function getType(): string
46
    {
47 83
        return Stringy::class;
48
    }
49
50
    /**
51
     * @return Stringy[]
52
     *
53
     * @phpstan-return array<array-key,Stringy>
54
     */
55
    public function getAll(): array
56
    {
57
        return parent::getAll();
58
    }
59
60
    /**
61
     * @return \Generator|Stringy[]
62
     *
63
     * @phpstan-return \Generator<mixed,Stringy>|\Generator<TKey,T>
64
     * @psalm-mutation-free
65
     */
66 82
    public function getGenerator(): \Generator
67
    {
68 82
        return parent::getGenerator();
69
    }
70
71
    /**
72
     * @return string[]
73
     */
74 2
    public function toStrings(): array
75
    {
76
        // init
77 2
        $result = [];
78
79 2
        foreach ($this->getArray() as $key => $value) {
80
            \assert($value instanceof Stringy);
81 2
            $result[$key] = $value->toString();
82
        }
83
84 2
        return $result;
85
    }
86
87
    /**
88
     * @param string ...$string
89
     *
90
     * @return $this
91
     */
92 1
    public function addString(string ...$string): self
93
    {
94 1
        foreach ($string as $stringTmp) {
95
            /** @phpstan-ignore-next-line | FP? */
96 1
            $this->add(Stringy::create($stringTmp));
97
        }
98
99 1
        return $this;
100
    }
101
102
    /**
103
     * @param Stringy ...$stringy
104
     *
105
     * @return $this
106
     */
107 1
    public function addStringy(Stringy ...$stringy): self
108
    {
109 1
        foreach ($stringy as $stringyTmp) {
110
            /** @phpstan-ignore-next-line | FP? */
111 1
            $this->add($stringyTmp);
112
        }
113
114 1
        return $this;
115
    }
116
117
    /**
118
     * @param string[] $strings
119
     *
120
     * @return static
121
     */
122 2
    public static function createFromStrings($strings = []): self
123
    {
124
        /** @noinspection AlterInForeachInspection */
125 2
        foreach ($strings as &$string) {
126 2
            $string = Stringy::create($string);
127
        }
128
129
        /** @noinspection PhpSillyAssignmentInspection */
130
        /** @var Stringy[] $strings */
131 2
        $strings = $strings;
0 ignored issues
show
Bug introduced by
Why assign $strings to itself?

This checks looks for cases where a variable has been assigned to itself.

This assignement can be removed without consequences.

Loading history...
132
133 2
        return new static($strings);
134
    }
135
}
136