Completed
Pull Request — master (#26)
by Andres
02:23
created

AlternativesTrait::ifThenElse()   C

Complexity

Conditions 13
Paths 1

Size

Total Lines 43
Code Lines 25

Duplication

Lines 18
Ratio 41.86 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 25
c 1
b 0
f 0
nc 1
nop 1
dl 18
loc 43
rs 5.1234

How to fix   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 Comfort\Validator\Helper;
3
4
use Comfort\ValidationError;
5
use Comfort\Validator\AbstractValidator;
6
7
trait AlternativesTrait
8
{
9
    /**
10
     * Provide conditional validation in two structures:
11
     * - alternatives(validator, validator, validator)
12
     *      Atleast ONE of the supplied validators MUST be valid
13
     * - alternatives([['if' => validator, 'then' => <validator|value>']])
14
     *
15
     * @param $conditions
16
     * @return $this
17
     */
18
    public function alternatives()
19
    {
20
        $args = func_get_args();
21
        $numArgs = func_num_args();
22
23
        if ($numArgs == 1) {
24
            return $this->ifThenElse($args[0]);
25
        } else {
26
            return $this->orMethod($args);
27
        }
28
    }
29
30
    /**
31
     * Assess a list of validators
32
     * @param \Comfort\Validator\AbstractValidator[] $conditions
33
     */
34
    private function orMethod($conditions)
35
    {
36
        return $this->add(function ($value, $nameKey) use ($conditions) {
0 ignored issues
show
Bug introduced by
It seems like add() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
            $errors = [];
38
            foreach ($conditions as $condition) {
39
                if (!$condition instanceof AbstractValidator) {
40
                    return $this->createError('alternatives.malformed_condition', $value, $nameKey);
0 ignored issues
show
Bug introduced by
It seems like createError() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
41
                }
42
43
                $result = $condition($value);
44
                if ($result instanceof ValidationError) {
45
                    $errors[] = $result;
46
                }
47
            }
48
49
            if (count($errors) === count($conditions)) {
50
                return $this->createError('alternatives.failed_or', $value, $nameKey);
0 ignored issues
show
Bug introduced by
It seems like createError() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
51
            }
52
        });
53
    }
54
55
    /**
56
     * @param $conditions
57
     * @return mixed
58
     */
59
    private function ifThenElse($conditions)
60
    {
61
        return $this->add(function ($value, $nameKey) use ($conditions) {
0 ignored issues
show
Bug introduced by
It seems like add() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
62
63
            foreach ($conditions as $condition) {
64
                if (!isset($condition['is'])) {
65
                    return $this->createError('alternatives.missing_is', $value, $nameKey);
0 ignored issues
show
Bug introduced by
It seems like createError() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
66
                }
67
68
                if (!$condition['is'] instanceof AbstractValidator) {
69
                    return $this->createError('alternatives.invalid_is', $value, $nameKey);
0 ignored issues
show
Bug introduced by
It seems like createError() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
70
                }
71
72
                /** @var AbstractValidator $is */
73
                $is = $condition['is'];
74
                $is->toBool(true);
75
76
                if (!isset($condition['then'])) {
77
                    return $this->createError('alternatives.missing_then', $value, $nameKey);
0 ignored issues
show
Bug introduced by
It seems like createError() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
78
                }
79
80
                if ($is($value)) {
81 View Code Duplication
                    if ($condition['then'] instanceof AbstractValidator) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
                        $validationStack = $condition['then']->validationStack;
83
                        foreach ($validationStack as $validator) {
84
                            $this->validationStack[] = $validator;
0 ignored issues
show
Bug introduced by
The property validationStack does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
85
                        }
86
                    } elseif (!is_null($condition['then'])) {
87
                        return $condition['then'];
88
                    }
89 View Code Duplication
                } elseif (isset($condition['else'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
                    if ($condition['else'] instanceof AbstractValidator) {
91
                        $validationStack = $condition['else']->validationStack;
92
                        foreach ($validationStack as $validator) {
93
                            $this->validationStack[] = $validator;
94
                        }
95
                    } elseif (!is_null($condition['else'])) {
96
                        return $condition['else'];
97
                    }
98
                }
99
            }
100
        });
101
    }
102
}