Passed
Push — master ( 526641...bb46a0 )
by Andrey
45:50 queued 43:04
created

Result::table()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Support;
4
5
use Helldar\LaravelLangPublisher\Contracts\Result as ResultContract;
6
use Helldar\LaravelLangPublisher\Facades\Arr;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Helldar\LaravelLangPublisher\Support\Arr. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Str;
9
10
use function array_merge;
11
12
final class Result implements ResultContract
13
{
14
    /** @var \Illuminate\Console\Command */
15
    protected $output;
16
17
    protected $items = [];
18
19
    protected $message = 'No Data';
20
21 18
    public function setOutput(Command $output): self
22
    {
23 18
        $this->output = $output;
24
25 18
        return $this;
26
    }
27
28
    public function setMessage(string $no_data): self
29
    {
30
        $this->message = $no_data;
31
32
        return $this;
33
    }
34
35
    public function show()
36
    {
37
        empty($this->items) ? $this->warn() : $this->table();
38
    }
39
40
    public function merge(array $items): void
41
    {
42
        $this->items = array_merge($this->items, $items);
43
    }
44
45
    protected function warn(): void
46
    {
47
        $this->output->warn($this->message);
48
    }
49
50
    protected function table(): void
51
    {
52
        $this->output->table($this->headers(), $this->items());
53
    }
54
55
    protected function headers(): array
56
    {
57
        $item = Arr::first($this->items);
58
        $keys = Arr::keys($item);
59
60
        return Arr::transform($keys, function ($value) {
61
            return Str::title($value);
62
        });
63
    }
64
65
    protected function items(): array
66
    {
67
        return $this->items;
68
    }
69
}
70