Passed
Push — task/common-translation-packag... ( 0de03f...12df17 )
by Grant
08:22 queued 11s
created

PolyExistsRule::passes()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 2
1
<?php
2
3
namespace App\Services\Validation\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Illuminate\Support\Facades\Lang;
7
use Illuminate\Database\Eloquent\Relations\Relation;
8
9
class PolyExistsRule implements Rule
10
{
11
12
    /**
13
     * The value in the the _type column, sepecifying the type of a polymorphic relationship.
14
     * eg 'applicant' or 'application'
15
     *
16
     * @var string
17
     */
18
    protected $poly_type;
19
20
    /**
1 ignored issue
show
Coding Style Documentation introduced by
Doc comment for parameter "$poly_type" missing
Loading history...
21
     * Create a new rule instance.
22
     *
23
     * @return void
24
     */
25
    public function __construct($poly_type)
0 ignored issues
show
introduced by
Method \App\Services\Validation\Rules\PolyExistsRule::__construct() does not have parameter type hint nor @param annotation for its parameter $poly_type.
Loading history...
26
    {
27
        $this->poly_type = $poly_type;
28
    }
29
30
    /**
2 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$attribute" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$value" missing
Loading history...
31
     * Determine if $value is a valid id of the model specified in the _type polymorphic relation column.
32
     * An example of how to use this rule:
33
     *    'commentable_id' => [new PolyTypeRule('user')]
34
     *
35
     */
1 ignored issue
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
36
    public function passes($attribute, $value)
1 ignored issue
show
introduced by
Method \App\Services\Validation\Rules\PolyExistsRule::passes() does not have parameter type hint nor @param annotation for its parameter $attribute.
Loading history...
introduced by
Method \App\Services\Validation\Rules\PolyExistsRule::passes() does not have parameter type hint nor @param annotation for its parameter $value.
Loading history...
introduced by
Method \App\Services\Validation\Rules\PolyExistsRule::passes() does not have return type hint nor @return annotation for its return value.
Loading history...
37
    {
38
        $type = $this->poly_type;
39
40
        if (Relation::getMorphedModel($type)) {
41
            $type = Relation::getMorphedModel($type);
42
        }
43
44
        if (!class_exists($type)) {
45
            return false;
46
        }
47
48
        return !empty(resolve($type)->find($value));
0 ignored issues
show
Bug introduced by
The method find() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        return !empty(resolve($type)->/** @scrutinizer ignore-call */ find($value));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
    }
50
51
    /**
52
     * Get the validation error message.
53
     *
54
     * @return string
55
     */
56
    public function message()
0 ignored issues
show
introduced by
Method \App\Services\Validation\Rules\PolyExistsRule::message() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
57
    {
58
        return Lang::get('validation.invalid_id');
59
    }
60
}
61