| Conditions | 1 |
| Paths | 1 |
| Total Lines | 60 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 66 | public function testComponentReturnsResponse() |
||
| 67 | { |
||
| 68 | $requestStack = new RequestStack(); |
||
| 69 | $routes = new RouteCollectionBuilder(); // Because I know how to use it. |
||
| 70 | $routes->add('/', EngineAsArgumentController::class, 'index'); // returns Symfony/Component/Routing/Route |
||
| 71 | $dispatcher = new EventDispatcher(); |
||
| 72 | $dispatcher->addSubscriber(new RouterListener( |
||
| 73 | new UrlMatcher( |
||
| 74 | $routes->build(), |
||
| 75 | new RequestContext() |
||
| 76 | ), |
||
| 77 | $requestStack |
||
| 78 | )); |
||
| 79 | $dispatcher->addSubscriber(new ResponseListener('UTF-8')); |
||
| 80 | |||
| 81 | $c = new ContainerBuilder(); |
||
| 82 | // https://symfony.com/doc/current/service_container.html |
||
| 83 | |||
| 84 | $c->autowire(TemplateNameParser::class) |
||
| 85 | ->setAutoconfigured(true) |
||
| 86 | ->setPublic(false); |
||
| 87 | $c->setAlias(TemplateNameParserInterface::class, TemplateNameParser::class); |
||
| 88 | |||
| 89 | $c->autowire(Twig_Loader_Array::class, Twig_Loader_Array::class) |
||
| 90 | ->setArgument('$templates', ['index.html.twig' => 'Hello Component!']) |
||
| 91 | ->setAutoconfigured(true) |
||
| 92 | ->setPublic(false); |
||
| 93 | $c->setAlias(Twig_LoaderInterface::class, Twig_Loader_Array::class); |
||
| 94 | |||
| 95 | $c->autowire(Twig_Environment::class, Twig_Environment::class) |
||
| 96 | ->setAutoconfigured(true) |
||
| 97 | ->setPublic(false); |
||
| 98 | $c->setAlias(Twig\Environment::class, Twig_Environment::class); |
||
| 99 | |||
| 100 | $c->autowire(TwigEngine::class) |
||
| 101 | ->setAutoconfigured(true) |
||
| 102 | ->setPublic(false); |
||
| 103 | $c->setAlias(EngineInterface::class, TwigEngine::class); |
||
| 104 | |||
| 105 | // Unit Testing |
||
| 106 | // $c->autowire('test.client', Client::class) |
||
| 107 | // ->setPublic(true); // Public needed! |
||
| 108 | |||
| 109 | //Controllers |
||
| 110 | $c->autowire(EngineAsArgumentController::class) |
||
| 111 | ->setAutoconfigured(true) |
||
| 112 | ->addTag('controller.service_arguments') |
||
| 113 | ->setPublic(false); |
||
| 114 | |||
| 115 | $this->assertInstanceOf( |
||
| 116 | // Response::class, // 5.4 < php |
||
| 117 | 'Symfony\Component\HttpFoundation\Response', |
||
| 118 | (new HttpKernel( |
||
| 119 | $dispatcher, |
||
| 120 | new ControllerResolver($c), |
||
| 121 | $requestStack, |
||
| 122 | new ArgumentResolver() |
||
| 123 | ))->handle(Request::create('/', 'GET')) |
||
| 124 | ); |
||
| 125 | } |
||
| 126 | |||
| 131 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: