Completed
Push — 3.x ( 3d5935...36902d )
by Jordi Sala
02:59
created

tests/App/AppKernel.php (1 issue)

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\App;
15
16
use Knp\Bundle\MenuBundle\KnpMenuBundle;
17
use Sonata\AdminBundle\SonataAdminBundle;
18
use Sonata\BlockBundle\SonataBlockBundle;
19
use Sonata\CoreBundle\SonataCoreBundle;
20
use Sonata\Doctrine\Bridge\Symfony\Bundle\SonataDoctrineBundle;
21
use Sonata\Form\Bridge\Symfony\SonataFormBundle;
22
use Sonata\Twig\Bridge\Symfony\SonataTwigBundle;
23
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
24
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
25
use Symfony\Bundle\SecurityBundle\SecurityBundle;
26
use Symfony\Bundle\TwigBundle\TwigBundle;
27
use Symfony\Component\Config\Loader\LoaderInterface;
28
use Symfony\Component\DependencyInjection\ContainerBuilder;
29
use Symfony\Component\HttpKernel\Kernel;
30
use Symfony\Component\Routing\RouteCollectionBuilder;
31
32
final class AppKernel extends Kernel
33
{
34
    use MicroKernelTrait;
35
36
    public function __construct()
37
    {
38
        parent::__construct('test', false);
39
    }
40
41
    public function registerBundles()
42
    {
43
        $bundles = [
44
            new FrameworkBundle(),
45
            new TwigBundle(),
46
            new SecurityBundle(),
47
            new KnpMenuBundle(),
48
            new SonataBlockBundle(),
49
            new SonataDoctrineBundle(),
50
            new SonataAdminBundle(),
51
            new SonataTwigBundle(),
52
            new SonataFormBundle(),
53
        ];
54
55
        if (class_exists(SonataCoreBundle::class)) {
56
            $bundles[] = new SonataCoreBundle();
57
        }
58
59
        return $bundles;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $bundles; (array<Symfony\Bundle\Fra...undle\SonataCoreBundle>) is incompatible with the return type declared by the interface Symfony\Component\HttpKe...erface::registerBundles of type Symfony\Component\HttpKe...undle\BundleInterface[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
60
    }
61
62
    public function getCacheDir(): string
63
    {
64
        return $this->getBaseDir().'cache';
65
    }
66
67
    public function getLogDir(): string
68
    {
69
        return $this->getBaseDir().'log';
70
    }
71
72
    public function getProjectDir()
73
    {
74
        return __DIR__;
75
    }
76
77
    protected function configureRoutes(RouteCollectionBuilder $routes)
78
    {
79
        $routes->import($this->getProjectDir().'/config/routes.yml');
80
    }
81
82
    protected function configureContainer(ContainerBuilder $containerBuilder, LoaderInterface $loader)
83
    {
84
        $containerBuilder->loadFromExtension('framework', [
85
            'secret' => 'MySecret',
86
            'fragments' => ['enabled' => true],
87
            'form' => ['enabled' => true],
88
            'session' => ['handler_id' => 'session.handler.native_file', 'storage_id' => 'session.storage.mock_file', 'name' => 'MOCKSESSID'],
89
            'assets' => null,
90
            'test' => true,
91
        ]);
92
93
        $containerBuilder->loadFromExtension('security', [
94
            'firewalls' => ['main' => ['anonymous' => true]],
95
            'providers' => ['in_memory' => ['memory' => null]],
96
        ]);
97
98
        $containerBuilder->loadFromExtension('twig', [
99
            'strict_variables' => '%kernel.debug%',
100
            'exception_controller' => null,
101
        ]);
102
103
        $loader->load($this->getProjectDir().'/config/services.yml');
104
    }
105
106
    private function getBaseDir(): string
107
    {
108
        return sys_get_temp_dir().'/sonata-admin-bundle/var/';
109
    }
110
}
111