Passed
Pull Request — master (#9)
by Rafael
03:29
created

TransformStringToBoolean   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 12
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 12
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transformArgument() 0 3 2
A supportsDefinitionAndArgument() 0 3 1
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Behat\Transformer;
12
13
use Behat\Behat\Definition\Call\DefinitionCall;
14
use Behat\Behat\Transformation\Transformer\ArgumentTransformer;
15
16
/**
17
 * Transform the world true and false to boolean,
18
 *
19
 * @example true => (bool) true
20
 * @example "true" => (bool) true
21
 * @example "false" => (bool) false
22
 *
23
 * @see     TransformAnyValueToString to scape types
24
 */
25
class TransformStringToBoolean implements ArgumentTransformer
26
{
27
    private const PATTERN = '/^true|false$/';
28
29
    public function supportsDefinitionAndArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue)
30
    {
31
        return preg_match(self::PATTERN, $argumentValue);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_match(self::PATTERN, $argumentValue) returns the type integer which is incompatible with the return type mandated by Behat\Behat\Transformati...DefinitionAndArgument() of boolean.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
32
    }
33
34
    public function transformArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue)
35
    {
36
        return 'true' === $argumentValue ? true : false;
37
    }
38
}
39