Issues (2)

src/ProvidesExtraFieldsValidator.php (2 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Laravel\ExtraFieldsValidator;
6
7
trait ProvidesExtraFieldsValidator
8
{
9
    public function validator()
10
    {
11
        return $this->makeExtraFieldsValidator();
12
    }
13
14
    protected function makeExtraFieldsValidator()
15
    {
16
        /** @var ValidationFactory $factory */
17
        $factory = $this->container->make(ValidationFactory::class);
18
        $rules = method_exists($this, 'rules') ? $this->container->call([$this, 'rules']) : [];
19
        $data = $this->getExtraFieldsValidationData();
20
21
        /** @var Validator $validator */
22
        $validator = $factory->make($data, $rules, $this->messages(), $this->attributes());
23
24
        return $validator->afterSuccess(function (Validator $validator) {
25
            /** @var ExtraFieldsDetector $detector */
26
            $detector = $this->container->make(
27
                config('extra-validator.extra_fields_detector', ExtraFieldsDetector::class)
28
            );
29
30
            $extraField = $detector->getFirstExtraField($validator->getData(), $validator->getRules());
31
32
            if (is_null($extraField)) {
33
                return true;
34
            }
35
36
            $validator->errors()->add($extraField, $this->getExtraFieldErrorMessage($extraField));
37
38
            return false;
39
        });
40
    }
41
42
    protected function getExtraFieldsValidationData()
43
    {
44
        switch (config('extra-validator.data_source', ExtraValidatorDataSource::DEFAULT)) {
45
            case ExtraValidatorDataSource::INPUT_SOURCE:
46
                return $this->getInputSource()->all();
0 ignored issues
show
It seems like getInputSource() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

46
                return $this->/** @scrutinizer ignore-call */ getInputSource()->all();
Loading history...
47
            default:
48
                return $this->validationData();
0 ignored issues
show
The method validationData() does not exist on Laravel\ExtraFieldsValid...desExtraFieldsValidator. Did you maybe mean validator()? ( 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 $this->/** @scrutinizer ignore-call */ validationData();

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
    public function getExtraFieldErrorMessage(string $field): string
53
    {
54
        return trans('validation.custom.extra_field', ['attribute' => $field]);
55
    }
56
}
57