Test Failed
Push — master ( 0cbd75...cf64e0 )
by Dmitry
02:49
created

AbstractTask::toArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 20
rs 9.9332
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Anticaptcha\Task;
4
5
use Anticaptcha\Entity;
6
use ReflectionClass;
7
use ReflectionProperty;
8
9
abstract class AbstractTask extends Entity
10
{
11
    public function getType(): string
12
    {
13
        $parts = explode('\\', static::class);
14
15
        return end($parts);
16
    }
17
18
    public function toArray(): array
19
    {
20
        $obj = new ReflectionClass(static::class);
21
22
        $attributes = [
23
            'type' => $this->getType()
24
        ];
25
26
        foreach ($obj->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
27
            $name = $reflectionProperty->getName();
28
            $value = $this->$name ?? null;
29
30
            if (is_null($value)) {
31
                continue;
32
            }
33
34
            $attributes[$name] = $value;
35
        }
36
37
        return $attributes;
38
    }
39
}
40