Completed
Push — master ( 52970f...c6d773 )
by Tristan
24:57 queued 10:40
created

ContainsObjectWithAttributeRule   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
wmc 6
eloc 12
dl 0
loc 48
ccs 11
cts 14
cp 0.7856
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A message() 0 3 1
A __construct() 0 4 1
A array_any() 0 7 3
A passes() 0 7 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace App\Services\Validation\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Illuminate\Support\Facades\Lang;
7
8
class ContainsObjectWithAttributeRule implements Rule
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ContainsObjectWithAttributeRule
Loading history...
9
{
10
11
    protected $attributeName;
12
    protected $attributeValue;
13
14
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $attributeName should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $attributeValue should have a doc-comment as per coding-style.
Loading history...
15
     * Create a new rule instance.
16
     *
17
     * @param  App\Models\Applicant  $applicant
0 ignored issues
show
Bug introduced by
The type App\Services\Validation\Rules\App\Models\Applicant was not found. Did you mean App\Models\Applicant? If so, make sure to prefix the type with \.
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Doc comment for parameter $applicant does not match actual variable name $attributeName
Loading history...
18
     * @param  string  $relation
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 15 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Doc comment for parameter $relation does not match actual variable name $attributeValue
Loading history...
19
     * @return void
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
20
     */
21 2
    public function __construct($attributeName, $attributeValue)
22
    {
23 2
        $this->attributeName = $attributeName;
24 2
        $this->attributeValue = $attributeValue;
25 2
    }
26
27 2
    protected function array_any(array $array, callable $fn) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function array_any()
Loading history...
Coding Style introduced by
Protected method name "ContainsObjectWithAttributeRule::array_any" is not in camel caps format
Loading history...
28 2
        foreach ($array as $value) {
29 2
            if($fn($value)) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
30 2
                return true;
31
            }
32
        }
33
        return false;
34
    }
35
36
    /**
37
     * This check passes if the $value is an array which contains an object
38
     *  with a attributeName relation equal to attributeValue
39
     * @param  [type] $attribute [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
40
     * @param  [type] $value     [description]
41
     * @return boolean            [description]
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
42
     */
43 2
    public function passes($attribute, $value)
44
    {
45
        // debugbar()->debug($value);
46
        // debugbar()->debug($this->attributeName);
47
        // debugbar()->debug($this->attributeValue);
48
        return $this->array_any($value, function ($object) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
49 2
            return $object[$this->attributeName] == $this->attributeValue;
50 2
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
51
    }
52
53
    public function message()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function message()
Loading history...
54
    {
55
        return Lang::trans('validation.contains_object_with_attribute', ['relation' => $this->attributeName, 'attributeValue' => $this->attributeValue]);
56
    }
57
}
58