Passed
Push — main ( ebbc7c...eb1d9a )
by Michael
04:10
created

HelpsFormatData::extractStringFromCollection()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 3
eloc 6
c 2
b 1
f 0
nc 4
nop 1
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\Formatters\Traits;
6
7
use Illuminate\Support\Collection;
8
9
trait HelpsFormatData
10
{
11
    /**
12
     * @param Collection $collection
13
     *
14
     * @return string
15
     */
16 7
    private function extractStringFromCollection(Collection $collection): string
17
    {
18 7
        $extracted = $collection->first();
19
20 7
        if (is_array($extracted)) {
21
            $extracted = current($extracted);
22
        }
23
24 7
        return is_string($extracted)
25 6
            ? $extracted
26 7
            : (string) $extracted;
27
    }
28
29
    /**
30
     * @param Collection $items
31
     *
32
     * @return Collection
33
     */
34
    private function getFirstFrom(Collection $items): Collection
35
    {
36
        return collect(
37
            $items->first()
38
        );
39
    }
40
}
41