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

DiffArgument::getTypeDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\TypeDefinition;
11
12
use Railt\Reflection\Base\Dependent\BaseArgument;
13
use Railt\Reflection\Contracts\Definitions\TypeDefinition;
14
use Railt\Reflection\Contracts\Dependent\FieldDefinition;
15
use Railt\SDL\Schema\CompilerInterface;
16
use Railt\SDL\Schema\Configuration;
17
18
/**
19
 * Class DiffArgument
20
 */
21
class DiffArgument extends BaseArgument
22
{
23
    public const ARGUMENT_NAME = 'diff';
24
    public const ARGUMENT_DESCRIPTION = '
25
        An argument that matches the date of the time difference.
26
        
27
        If a `NULL` is passed (or the value is not passed),
28
        then the current time is taken.
29
    ';
30
31
    /**
32
     * @var CompilerInterface|Configuration
33
     */
34
    private $compiler;
35
36
    /**
37
     * DiffArgument constructor.
38
     * @param FieldDefinition $field
39
     * @param CompilerInterface $compiler
40
     */
41
    public function __construct(FieldDefinition $field, CompilerInterface $compiler)
42
    {
43
        $this->compiler = $compiler;
44
        $this->name = self::ARGUMENT_NAME;
45
        $this->description = self::ARGUMENT_DESCRIPTION;
46
47
        $this->parent = $field;
48
49
        $this->defaultValue = null;
50
        $this->hasDefaultValue = true;
51
    }
52
53
    /**
54
     * @return TypeDefinition
55
     */
56
    public function getTypeDefinition(): TypeDefinition
57
    {
58
        return $this->compiler->getDictionary()->get('Carbon', $this);
0 ignored issues
show
Bug introduced by
The method getDictionary does only exist in Railt\SDL\Schema\Configuration, but not in Railt\SDL\Schema\CompilerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
59
    }
60
}
61