FormatArgument::getTypeDefinition()   A
last analyzed

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\CarbonExtension\In\CarbonFormat;
13
use Railt\SDL\Base\Dependent\BaseArgument;
14
use Railt\SDL\Contracts\Definitions\TypeDefinition;
15
use Railt\SDL\Contracts\Dependent\FieldDefinition;
16
use Railt\SDL\Schema\CompilerInterface;
17
use Railt\SDL\Schema\Configuration;
18
19
/**
20
 * Class FormatArgument
21
 */
22
class FormatArgument extends BaseArgument
23
{
24
    public const ARGUMENT_NAME        = 'format';
25
    public const ARGUMENT_DESCRIPTION = '
26
        An argument that provides a format of the given value that 
27
        are contained in a CarbonFormat enumeration type
28
    ';
29
30
    /**
31
     * @var CompilerInterface|Configuration
32
     */
33
    private $compiler;
34
35
    /**
36
     * DiffArgument constructor.
37
     * @param FieldDefinition $field
38
     * @param CompilerInterface $compiler
39
     */
40
    public function __construct(FieldDefinition $field, CompilerInterface $compiler)
41
    {
42
        $this->compiler    = $compiler;
43
        $this->name        = self::ARGUMENT_NAME;
44
        $this->description = self::ARGUMENT_DESCRIPTION;
45
46
        $this->parent = $field;
47
48
        $this->defaultValue    = CarbonFormat::DEFAULT;
49
        $this->hasDefaultValue = true;
50
    }
51
52
    /**
53
     * @return TypeDefinition
54
     */
55
    public function getTypeDefinition(): TypeDefinition
56
    {
57
        return $this->compiler->getDictionary()->get('CarbonFormat', $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...
58
    }
59
}
60