Passed
Push — master ( 19e483...625e7f )
by Mathieu
04:13
created

JsonExporter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 30
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A exportToJson() 0 3 1
A isExporting() 0 3 1
A getJsonExportableRelations() 0 3 1
A exportToCollection() 0 3 1
A getJsonExportableAttributes() 0 4 1
1
<?php
2
3
namespace MathieuTu\JsonSyncer\Traits;
4
5
use Illuminate\Support\Collection;
6
use MathieuTu\JsonSyncer\Helpers\JsonExporter as ExporterHelper;
7
use MathieuTu\JsonSyncer\Helpers\RelationsInModelFinder;
8
9
trait JsonExporter
10
{
11
    protected $jsonExportableRelations;
12
    protected $jsonExportableAttributes;
13
14 2
    public function exportToJson($jsonOptions = 0): string
15
    {
16 2
        return ExporterHelper::exportToJson($this, $jsonOptions);
0 ignored issues
show
Bug introduced by
$this of type MathieuTu\JsonSyncer\Traits\JsonExporter is incompatible with the type MathieuTu\JsonSyncer\Contracts\JsonExportable expected by parameter $exportable of MathieuTu\JsonSyncer\Hel...xporter::exportToJson(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

16
        return ExporterHelper::exportToJson(/** @scrutinizer ignore-type */ $this, $jsonOptions);
Loading history...
17
    }
18
19 1
    public function exportToCollection(): Collection
20
    {
21 1
        return ExporterHelper::exportToCollection($this);
0 ignored issues
show
Bug introduced by
$this of type MathieuTu\JsonSyncer\Traits\JsonExporter is incompatible with the type MathieuTu\JsonSyncer\Contracts\JsonExportable expected by parameter $exportable of MathieuTu\JsonSyncer\Hel...r::exportToCollection(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

21
        return ExporterHelper::exportToCollection(/** @scrutinizer ignore-type */ $this);
Loading history...
22
    }
23
24
    public function getJsonExportableAttributes(): array
25
    {
26 3
        return $this->jsonExportableAttributes ?? array_filter($this->getFillable(), function ($attribute) {
0 ignored issues
show
Bug introduced by
It seems like getFillable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        return $this->jsonExportableAttributes ?? array_filter($this->/** @scrutinizer ignore-call */ getFillable(), function ($attribute) {
Loading history...
27 3
            return !ends_with($attribute, '_id');
28 3
        });
29
    }
30
31 2
    public function getJsonExportableRelations(): array
32
    {
33 2
        return $this->jsonExportableRelations ?? RelationsInModelFinder::hasOneOrMany($this);
0 ignored issues
show
Bug introduced by
$this of type MathieuTu\JsonSyncer\Traits\JsonExporter is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $model of MathieuTu\JsonSyncer\Hel...lFinder::hasOneOrMany(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        return $this->jsonExportableRelations ?? RelationsInModelFinder::hasOneOrMany(/** @scrutinizer ignore-type */ $this);
Loading history...
34
    }
35
36 1
    public function isExporting(): bool
37
    {
38 1
        return collect(debug_backtrace())->contains('class', ExporterHelper::class);
39
    }
40
}
41