Passed
Push — master ( 86adb7...607b33 )
by Kirill
02:06
created

Extension::extend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\CarbonExtension;
11
12
use Railt\CarbonExtension\TypeDefinition\DiffArgument;
13
use Railt\CarbonExtension\TypeDefinition\FormatArgument;
14
use Railt\Foundation\Events\TypeBuilding;
15
use Railt\Foundation\Extensions\BaseExtension;
16
use Railt\Io\File;
17
use Railt\Reflection\Contracts\Definitions\InputDefinition;
18
use Railt\Reflection\Contracts\Definitions\TypeDefinition;
19
use Railt\Reflection\Contracts\Dependent\FieldDefinition;
20
use Railt\Routing\Contracts\RouterInterface;
21
use Railt\SDL\Reflection\Builder\Dependent\FieldBuilder;
22
use Railt\SDL\Schema\CompilerInterface as Compiler;
23
use Symfony\Component\EventDispatcher\EventDispatcherInterface as Events;
24
25
/**
26
 * Class Extension
27
 */
28
class Extension extends BaseExtension
29
{
30
    /**
31
     * @var string Path to graphql schema file
32
     */
33
    private const GRAPHQL_DATETIME = __DIR__ . '/../resources/datetime.graphqls';
34
35
    /**
36
     * @param Compiler $compiler
37
     * @throws \Railt\Io\Exception\NotReadableException
38
     */
39
    public function boot(Compiler $compiler): void
40
    {
41
        $compiler->compile(File::fromPathname(self::GRAPHQL_DATETIME));
0 ignored issues
show
Bug introduced by
It seems like \Railt\Io\File::fromPath...self::GRAPHQL_DATETIME) targeting Railt\Io\File::fromPathname() can also be of type object<Railt\Io\File>; however, Railt\SDL\Schema\CompilerInterface::compile() does only seem to accept object<Railt\Io\Readable>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
42
43
        $this->call(\Closure::fromCallable([$this, 'listen']));
44
    }
45
46
    /**
47
     * @param Events $listener
48
     * @param Compiler $compiler
49
     * @param RouterInterface $router
50
     */
51
    private function listen(Events $listener, Compiler $compiler, RouterInterface $router): void
52
    {
53
        $controller = $this->make(CarbonController::class);
54
55
        $invoke = function(TypeBuilding $event) use ($compiler, $router, $controller) {
56
            if ($this->isCarbonField($event->getType())) {
57
                $this->extend($compiler, $event->getType());
0 ignored issues
show
Compatibility introduced by
$event->getType() of type object<Railt\Reflection\...nitions\TypeDefinition> is not a sub-type of object<Railt\Reflection\...endent\FieldDefinition>. It seems like you assume a child interface of the interface Railt\Reflection\Contrac...initions\TypeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
58
59
                $router->route($event->getType())->then(\Closure::fromCallable([$controller, 'getDateTime']));
0 ignored issues
show
Compatibility introduced by
$event->getType() of type object<Railt\Reflection\...nitions\TypeDefinition> is not a sub-type of object<Railt\Reflection\...endent\FieldDefinition>. It seems like you assume a child interface of the interface Railt\Reflection\Contrac...initions\TypeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
60
            }
61
        };
62
63
64
        $listener->addListener(TypeBuilding::class, $invoke);
65
    }
66
67
    /**
68
     * @param Compiler $compiler
69
     * @param FieldDefinition|FieldBuilder|TypeDefinition $field
70
     */
71
    private function extend(Compiler $compiler, FieldDefinition $field): void
72
    {
73
        (function() use ($field, $compiler) {
74
            $this->arguments[DiffArgument::ARGUMENT_NAME] = new DiffArgument($field, $compiler);
0 ignored issues
show
Bug introduced by
The property arguments 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...
75
            $this->arguments[FormatArgument::ARGUMENT_NAME] = new FormatArgument($field, $compiler);
76
        })->call($field);
77
    }
78
79
    /**
80
     * @param TypeDefinition $type
81
     * @return bool
82
     */
83
    private function isCarbonField(TypeDefinition $type): bool
84
    {
85
        return $type instanceof FieldDefinition && $type->getTypeDefinition()->getName() === 'Carbon';
86
    }
87
}
88