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

JsonExporter::isExporting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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