Completed
Push — master ( 5d51b4...87fdd9 )
by Paul
10s
created

Bundle/FormBundle/Form/Type/UrlvalidatedType.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Victoire\Bundle\FormBundle\Form\Type;
4
5
use Symfony\Bundle\FrameworkBundle\Routing\Router;
6
use Symfony\Component\Form\AbstractType;
7
use Symfony\Component\Form\FormInterface;
8
use Symfony\Component\Form\FormView;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
use Victoire\Bundle\CoreBundle\Entity\View;
11
12
/**
13
 * Form field with some slug validation and domain prefix.
14
 */
15
class UrlvalidatedType extends AbstractType
16
{
17
    /**
18
     * @var Router
19
     */
20
    private $router;
21
22
    public function __construct(Router $router)
23
    {
24
        $this->router = $router;
25
    }
26
27
    public function buildView(FormView $view, FormInterface $form, array $options)
28
    {
29
        $locale = null;
30
        while ($form && !($page = $form->getData()) instanceof View) {
31
            $form = $form->getParent();
32
            // when the form is an a2lix_translationsFields, then it's name is the current locale,
33
            // we store it to generate the link in the good locale
34
            if ('a2lix_translationsFields' === $form->getConfig()->getType()->getName()) {
35
                $locale = $form->getName();
36
            }
37
        }
38
39
        if (!$locale) {
40
            $locale = $page->getCurrentLocale();
0 ignored issues
show
The variable $page does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
41
        }
42
43
        $url = $this->router->generate('victoire_core_page_show', ['url' => $page->getParent()->getUrl(), '_locale' => $locale], Router::ABSOLUTE_URL);
44
        $view->vars['base_url'] = $url;
45
46
        parent::buildView($view, $form, $options);
0 ignored issues
show
It seems like $form defined by $form->getParent() on line 31 can be null; however, Symfony\Component\Form\AbstractType::buildView() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
47
    }
48
49
    public function configureOptions(OptionsResolver $resolver)
50
    {
51
        parent::configureOptions($resolver);
52
    }
53
54
    public function getParent()
55
    {
56
        return SlugType::class;
57
    }
58
}
59