Passed
Push — master ( 54c1b4...a67e0e )
by Vladislav
11:20 queued 09:15
created

AbstractParameters::array()   B

Complexity

Conditions 11
Paths 4

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 51
rs 7.3166
cc 11
nc 4
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Carpenstar\ByBitAPI\Core\Objects;
3
4
use Carpenstar\ByBitAPI\Core\Exceptions\SDKException;
5
use Carpenstar\ByBitAPI\Core\Helpers\DateTimeHelper;
6
use Carpenstar\ByBitAPI\Core\Interfaces\IParametersInterface;
7
8
abstract class AbstractParameters implements IParametersInterface
9
{
10
    protected array $requiredFields = [];
11
12
    protected array $requiredBetweenFields = [];
13
14
    /**
15
     * @return array
16
     */
17
    public function array(): array
18
    {
19
        $entity = $this;
20
        $entityMethods = get_class_methods($this);
21
        $params = [];
22
23
        array_walk($entityMethods, function ($method) use (&$entity, &$params) {
24
            if (substr($method, 0, 3) == 'get') {
25
                $entityProperty = lcfirst(substr($method, 3));
26
                if (isset($entity->$entityProperty)) {
27
28
                    if ($entity->$method() instanceof \DateTime) {
29
                        $ePropertyVal = DateTimeHelper::makeTimestampFromDateString($entity->$method()->format("Y-m-d H:i:s"));
30
                    } else {
31
                        $ePropertyVal = (string)$entity->$method();
32
                    }
33
34
                    $params[$entityProperty] = $ePropertyVal;
35
36
                    $propIndex = array_search($entityProperty, $entity->requiredFields, true);
37
38
                    if($propIndex > -1) {
39
                        unset($entity->requiredFields[$propIndex]);
40
                    }
41
42
                    if (!empty($entity->requiredBetweenFields)) {
43
                        foreach ($entity->requiredBetweenFields as $index => $condition) {
44
                            if (in_array($entityProperty, $condition)) {
45
                                unset($entity->requiredBetweenFields[$index]);
46
                                break;
47
                            }
48
                        }
49
                    }
50
                }
51
            }
52
        });
53
54
        if (!empty($this->requiredFields)) {
55
            throw new SDKException(sprintf(SDKException::EXCEPTION_REQUIRED_FIELD_TEXT, implode(',', $this->requiredFields)));
56
        }
57
58
        if (!empty($this->requiredBetweenFields)) {
59
            $paramsString = '';
60
            foreach ($this->requiredBetweenFields as $fieldArray) {
61
                $paramsString .= implode(' or ', $fieldArray);
62
            }
63
            $params = $paramsString;
0 ignored issues
show
Unused Code introduced by
The assignment to $params is dead and can be removed.
Loading history...
64
65
            throw new SDKException(sprintf(SDKException::EXCEPTION_REQUIRED_SPECIFY_BETWEEN_FIELDS, $paramsString));
66
        }
67
        return $params;
68
    }
69
70
    protected function setRequiredField(string $fieldName): self
71
    {
72
        $this->requiredFields[] = $fieldName;
73
        return $this;
74
    }
75
76
    protected function setRequiredBetweenField(string $fieldOne, string $fieldTwo): self
77
    {
78
        $this->requiredBetweenFields[] = [$fieldOne, $fieldTwo];
79
        return $this;
80
    }
81
}