Passed
Push — main ( 5926a8...a239d4 )
by Garbuz
03:10
created

Field::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
cc 2
nc 2
nop 2
crap 2.0625
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GarbuzIvan\LaravelGeneratorPackage\Form;
6
7
use GarbuzIvan\LaravelGeneratorPackage\Configuration;
8
use GarbuzIvan\LaravelGeneratorPackage\Contracts\FieldInterface;
9
use GarbuzIvan\LaravelGeneratorPackage\Exceptions\FieldDoesNotExistsException;
10
11
class Field
12
{
13
    protected Configuration $config;
14
15
    /**
16
     * Form constructor.
17
     * @param Configuration $config
18
     */
19 15
    public function __construct(Configuration $config)
20
    {
21 15
        $this->config = $config;
22 15
    }
23
24
    /**
25
     * Find field class.
26
     *
27
     * @param string $method
28
     * @return bool|mixed
29
     */
30 15
    public function findFieldClass(string $method)
31
    {
32 15
        if (is_null($method)) {
0 ignored issues
show
introduced by
The condition is_null($method) is always false.
Loading history...
33
            return false;
34
        }
35 15
        $fields = $this->config->getFields();
36 15
        if (isset($fields[$method])) {
37 15
            $class = $fields[$method];
38
        }
39 15
        if (isset($class) && class_exists($class)) {
40 15
            return $class;
41
        }
42
        return false;
43
    }
44
45
    /**
46
     * Generate a Field object and add to form builder if Field exists.
47
     *
48
     * @param string $method
49
     * @param $args
50
     * @return FieldInterface
51
     * @throws FieldDoesNotExistsException
52
     */
53 15
    public function __call(string $method, array $args): FieldInterface
54
    {
55 15
        if ($className = $this->findFieldClass($method)) {
56 15
            return app($className)->init($args);
0 ignored issues
show
Bug introduced by
The method init() 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

56
            return app($className)->/** @scrutinizer ignore-call */ init($args);

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...
57
        }
58
        throw new FieldDoesNotExistsException();
59
    }
60
}
61