Extension   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 10
dl 0
loc 59
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 6 1
A listen() 0 15 2
A isCarbonField() 0 4 2
A extend() 0 6 1
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\FormatArgument;
13
use Railt\Foundation\Events\TypeBuilding;
14
use Railt\Foundation\Extensions\BaseExtension;
15
use Railt\Io\File;
16
use Railt\Routing\Contracts\RouterInterface;
17
use Railt\SDL\Contracts\Definitions\TypeDefinition;
18
use Railt\SDL\Contracts\Dependent\FieldDefinition;
19
use Railt\SDL\Reflection\Builder\Dependent\FieldBuilder;
20
use Railt\SDL\Schema\CompilerInterface as Compiler;
21
use Symfony\Component\EventDispatcher\EventDispatcherInterface as Events;
22
23
/**
24
 * Class Extension
25
 */
26
class Extension extends BaseExtension
27
{
28
    /**
29
     * @var string Path to graphql schema file
30
     */
31
    private const GRAPHQL_DATETIME = __DIR__ . '/../resources/datetime.graphqls';
32
33
    /**
34
     * @param Compiler $compiler
35
     * @throws \Railt\Io\Exception\NotReadableException
36
     */
37
    public function boot(Compiler $compiler): void
38
    {
39
        $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...
40
41
        $this->call(\Closure::fromCallable([$this, 'listen']));
42
    }
43
44
    /**
45
     * @param Events $listener
46
     * @param Compiler $compiler
47
     * @param RouterInterface $router
48
     */
49
    private function listen(Events $listener, Compiler $compiler, RouterInterface $router): void
50
    {
51
        $controller = $this->make(CarbonController::class);
52
53
        $invoke = function (TypeBuilding $event) use ($compiler, $router, $controller): void {
54
            if ($this->isCarbonField($event->getType())) {
55
                $this->extend($compiler, $event->getType());
0 ignored issues
show
Compatibility introduced by
$event->getType() of type object<Railt\SDL\Contrac...nitions\TypeDefinition> is not a sub-type of object<Railt\SDL\Contrac...endent\FieldDefinition>. It seems like you assume a child interface of the interface Railt\SDL\Contracts\Definitions\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...
56
57
                $router->route($event->getType())->then(\Closure::fromCallable([$controller, 'getDateTime']));
0 ignored issues
show
Compatibility introduced by
$event->getType() of type object<Railt\SDL\Contrac...nitions\TypeDefinition> is not a sub-type of object<Railt\SDL\Contrac...endent\FieldDefinition>. It seems like you assume a child interface of the interface Railt\SDL\Contracts\Definitions\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
        };
60
61
62
        $listener->addListener(TypeBuilding::class, $invoke);
63
    }
64
65
    /**
66
     * @param TypeDefinition $type
67
     * @return bool
68
     */
69
    private function isCarbonField(TypeDefinition $type): bool
70
    {
71
        return $type instanceof FieldDefinition && $type->getTypeDefinition()->getName() === 'Carbon';
72
    }
73
74
    /**
75
     * @param Compiler $compiler
76
     * @param FieldDefinition|FieldBuilder|TypeDefinition $field
77
     */
78
    private function extend(Compiler $compiler, FieldDefinition $field): void
79
    {
80
        (function () use ($field, $compiler): void {
81
            $this->arguments[FormatArgument::ARGUMENT_NAME] = new FormatArgument($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...
82
        })->call($field);
83
    }
84
}
85