Result::headers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 0
cts 4
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\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...
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Str;
8
9
final class Result
10
{
11
    /** @var \Illuminate\Console\Command */
12
    protected $output;
13
14
    protected $items = [];
15
16
    protected $message = 'No Data';
17
18 36
    public function setOutput(Command $output): self
19
    {
20 36
        $this->output = $output;
21
22 36
        return $this;
23
    }
24
25
    public function setMessage(string $no_data): self
26
    {
27
        $this->message = $no_data;
28
29
        return $this;
30
    }
31
32
    public function show()
33
    {
34
        empty($this->items) ? $this->warn() : $this->table();
35
    }
36
37
    public function merge(array $items): void
38
    {
39
        $this->items = array_merge($this->items, $items);
40
    }
41
42
    protected function warn(): void
43
    {
44
        $this->output->warn($this->message);
45
    }
46
47
    protected function table(): void
48
    {
49
        $this->output->table($this->headers(), $this->items());
50
    }
51
52
    protected function headers(): array
53
    {
54
        $item = Arr::first($this->items);
55
        $keys = Arr::keys($item);
56
57
        return Arr::transform($keys, function ($value) {
58
            return Str::title($value);
59
        });
60
    }
61
62
    protected function items(): array
63
    {
64
        return $this->items;
65
    }
66
}
67