Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — 0.9 (#692)
by
unknown
02:58
created

Sf   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.3%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 56
ccs 26
cts 27
cp 0.963
rs 10
c 2
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A returnViolationsForConstraint() 0 6 1
A __construct() 0 5 1
A createSymfonyConstraint() 0 16 3
A assert() 0 9 2
A validate() 0 9 2
1
<?php
2
namespace Respect\Validation\Rules;
3
4
use ReflectionClass;
5
use ReflectionException;
6
use Respect\Validation\Exceptions\ComponentException;
7
use Symfony\Component\Validator\Validation;
8
use Symfony\Component\Validator\Constraint;
9
10
class Sf extends AbstractRule
11
{
12
    const SYMFONY_CONSTRAINT_NAMESPACE = 'Symfony\Component\Validator\Constraints\%s';
13
    public $name;
14
    private $constraint;
15
16 4
    public function __construct($name, $params = array())
17
    {
18 4
        $this->name = ucfirst($name);
19 4
        $this->constraint = $this->createSymfonyConstraint($this->name, $params);
20 3
    }
21
22 4
    private function createSymfonyConstraint($constraintName, $constraintConstructorParameters = array())
23
    {
24 4
        $fullClassName = sprintf(self::SYMFONY_CONSTRAINT_NAMESPACE, $constraintName);
25
        try {
26 4
            $constraintReflection = new ReflectionClass($fullClassName);
27 4
        } catch (ReflectionException $previousException) {
28 1
            $baseExceptionMessage = 'Symfony/Validator constraint "%s" does not exist.';
29 1
            $exceptionMessage = sprintf($baseExceptionMessage, $constraintName);
30 1
            throw new ComponentException($exceptionMessage, 0, $previousException);
31
        }
32 3
        if ($constraintReflection->hasMethod('__construct')) {
33 3
            return $constraintReflection->newInstanceArgs($constraintConstructorParameters);
34
        }
35
36
        return $constraintReflection->newInstance();
37
    }
38
39 3
    private function returnViolationsForConstraint($valueToValidate, Constraint $symfonyConstraint)
40
    {
41 3
        $validator = Validation::createValidator(); // You gotta love those Symfony namings
42
43 3
        return $validator->validateValue($valueToValidate, $symfonyConstraint);
0 ignored issues
show
Bug introduced by
The method validateValue does only exist in Symfony\Component\Validator\Validator, but not in Symfony\Component\Valida...ator\RecursiveValidator.

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...
44
    }
45
46 2
    public function assert($input)
47
    {
48 2
        $violations = $this->returnViolationsForConstraint($input, $this->constraint);
49 2
        if (count($violations) == 0) {
50 1
            return true;
51
        }
52
53 1
        throw $this->reportError((string) $violations);
54
    }
55
56 1
    public function validate($input)
57
    {
58 1
        $violations = $this->returnViolationsForConstraint($input, $this->constraint);
59 1
        if (count($violations)) {
60 1
            return false;
61
        }
62
63 1
        return true;
64
    }
65
}
66