Completed
Push — master ( ff9432...54dd63 )
by Lars
02:28
created

Create.php ➔ collection()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 23

Duplication

Lines 23
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 5.2

Importance

Changes 0
Metric Value
cc 5
nc 7
nop 1
dl 23
loc 23
ccs 8
cts 10
cp 0.8
crap 5.2
rs 9.2408
c 0
b 0
f 0
1
<?php
2
3
namespace Stringy;
4
5
if (!\function_exists('Stringy\create')) {
6
    /**
7
     * Creates a Stringy object and returns it on success.
8
     *
9
     * @param mixed  $str      Value to modify, after being cast to string
10
     * @param string $encoding The character encoding
11
     *
12
     * @return Stringy A Stringy object
13
     *
14
     * @throws \InvalidArgumentException if an array or object without a
15
     *                                   __toString method is passed as the first argument
16
     *
17
     */
18
    function create($str, string $encoding = null)
19
    {
20
        return new Stringy($str, $encoding);
21
    }
22
}
23
24
if (!\function_exists('Stringy\collection')) {
25
    /**
26
     * @param string[]|Stringy[]|null $input
27
     *
28
     * @return CollectionStringy
29
     *
30
     * @throws \TypeError
31
     */
32 View Code Duplication
    function collection($input = null)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34
        // init
35 1
        $newCollection = new CollectionStringy();
36
37 1
        if ($input === null) {
38
            return $newCollection;
39
        }
40
41 1
        if (!\is_array($input)) {
42
            $input = [$input];
43
        }
44
45 1
        foreach ($input as &$stringOrStringy) {
46 1
            if (\is_string($stringOrStringy)) {
47 1
                $stringOrStringy = new Stringy($stringOrStringy);
48
            }
49
50 1
            $newCollection[] = $stringOrStringy;
51
        }
52
53 1
        return $newCollection;
54
    }
55
}
56