JsonExporter::isExporting()   A
last analyzed

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 Illuminate\Support\Str;
7
use MathieuTu\JsonSyncer\Helpers\JsonExporter as ExporterHelper;
8
use MathieuTu\JsonSyncer\Helpers\RelationsInModelFinder;
9
10
trait JsonExporter
11
{
12
    protected $jsonExportableRelations;
13
    protected $jsonExportableAttributes;
14
15 2
    public function exportToJson($jsonOptions = 0): string
16
    {
17 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

17
        return ExporterHelper::exportToJson(/** @scrutinizer ignore-type */ $this, $jsonOptions);
Loading history...
18
    }
19
20 1
    public function exportToCollection(): Collection
21
    {
22 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

22
        return ExporterHelper::exportToCollection(/** @scrutinizer ignore-type */ $this);
Loading history...
23
    }
24
25 3
    public function getJsonExportableAttributes(): array
26
    {
27 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

27
        return $this->jsonExportableAttributes ?? array_filter($this->/** @scrutinizer ignore-call */ getFillable(), function ($attribute) {
Loading history...
28 3
            return ! Str::endsWith($attribute, '_id');
29 3
        });
30
    }
31
32 2
    public function getJsonExportableRelations(): array
33
    {
34 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

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