Test Setup Failed
Push — master ( a515d0...24a3ce )
by Raí
07:35 queued 02:54
created

ToArrayTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
dl 0
loc 63
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C toArray() 0 61 15
1
<?php
2
3
namespace Bludata\Doctrine\ORM\Traits;
4
5
trait ToArrayTrait
6
{
7
    public function toArray(array $options = []): array
8
    {
9
    	$reflectionProperty  = new ReflectionProperty(get_called_class(), $key);
0 ignored issues
show
Bug introduced by
The type Bludata\Doctrine\ORM\Traits\ReflectionProperty was not found. Did you mean ReflectionProperty? If so, make sure to prefix the type with \.
Loading history...
Comprehensibility Best Practice introduced by
The variable $key seems to be never defined.
Loading history...
10
        $annotationReader    = new AnnotationReader();
0 ignored issues
show
Bug introduced by
The type Bludata\Doctrine\ORM\Traits\AnnotationReader was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
        $propertyAnnotations = $annotationReader->getPropertyAnnotations($reflectionProperty);
0 ignored issues
show
Unused Code introduced by
The assignment to $propertyAnnotations is dead and can be removed.
Loading history...
12
        
13
        $array = [];
14
15
        $classMetadata = $this->getRepository()
0 ignored issues
show
Bug introduced by
It seems like getRepository() 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

15
        $classMetadata = $this->/** @scrutinizer ignore-call */ getRepository()
Loading history...
16
                              ->getClassMetadata();
17
18
        foreach ($this->getFillable() as $key) {
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

18
        foreach ($this->/** @scrutinizer ignore-call */ getFillable() as $key) {
Loading history...
19
            $metaDataKey = $classMetadata->hasField($key) ? $classMetadata->getFieldMapping($key) : null;
20
21
            if ($this->checkOnyExceptInArray($key, $options)) {
0 ignored issues
show
Bug introduced by
It seems like checkOnyExceptInArray() 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

21
            if ($this->/** @scrutinizer ignore-call */ checkOnyExceptInArray($key, $options)) {
Loading history...
22
                if (is_object($this->$key)) {
23
                    if ($this->$key instanceof DateTime) {
0 ignored issues
show
Bug introduced by
The type Bludata\Doctrine\ORM\Traits\DateTime was not found. Did you mean DateTime? If so, make sure to prefix the type with \.
Loading history...
24
                        if ($this->$key) {
25
                            $dateFormat = 'Y-m-d';
26
27
                            if ($metaDataKey) {
28
                                switch ($metaDataKey['type']) {
29
                                    case 'datetime':
30
                                        $dateFormat = 'Y-m-d H:i:s';
31
                                        break;
32
                                    case 'time':
33
                                        $dateFormat = 'H:i:s';
34
                                        break;
35
                                    default:
36
                                        break;
37
                                }
38
                            }
39
40
                            $array[$key] = $this->$key->format($dateFormat);
41
                        }
42
                    } elseif ($this->$key instanceof ArrayCollection || $this->$key instanceof PersistentCollection) {
0 ignored issues
show
Bug introduced by
The type Bludata\Doctrine\ORM\Traits\ArrayCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type Bludata\Doctrine\ORM\Traits\PersistentCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
                        $ids = [];
44
45
                        foreach ($this->$key->getValues() as $item) {
46
                            $ids[] = $item->getId();
47
                        }
48
49
                        $array[$key] = $ids;
50
                    } else {
51
                        if (method_exists($this->$key, 'getId')) {
52
                            $array[$key] = $this->$key->getId();
53
                        } else {
54
                            $array[$key] = $this->$key;
55
                        }
56
                    }
57
                } else {
58
                    if ($metaDataKey['type'] == 'decimal') {
59
                        $array[$key] = (float) $this->$key;
60
                    } else {
61
                        $array[$key] = $this->$key;
62
                    }
63
                }
64
            }
65
        }
66
67
        return $array;
68
    }
69
}
70