Conditions | 2 |
Paths | 2 |
Total Lines | 61 |
Code Lines | 44 |
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 |
||
62 | public function testComponentReturnsResponse() |
||
63 | { |
||
64 | $requestStack = new RequestStack(); |
||
65 | $routes = new RouteCollectionBuilder(); // Because I know how to use it. |
||
66 | $routes->add('/', EngineAsArgumentController::class, 'index'); // returns Symfony/Component/Routing/Route |
||
67 | $dispatcher = new EventDispatcher(); |
||
68 | $dispatcher->addSubscriber(new RouterListener( |
||
69 | new UrlMatcher( |
||
70 | $routes->build(), |
||
71 | new RequestContext() |
||
72 | ), |
||
73 | $requestStack |
||
74 | )); |
||
75 | $dispatcher->addSubscriber(new ResponseListener('UTF-8')); |
||
76 | |||
77 | $c = new ContainerBuilder(); |
||
78 | // https://symfony.com/doc/current/service_container.html |
||
79 | |||
80 | $c->autowire(TemplateNameParser::class) |
||
81 | ->setAutoconfigured(true) |
||
82 | ->setPublic(false); |
||
83 | $c->setAlias(TemplateNameParserInterface::class, TemplateNameParser::class); |
||
84 | |||
85 | $c->autowire(Twig_Loader_Array::class, Twig_Loader_Array::class) |
||
86 | ->setArgument('$templates', ['index.html.twig' => 'Hello Component!']) |
||
87 | ->setAutoconfigured(true) |
||
88 | ->setPublic(false); |
||
89 | $c->setAlias(Twig_LoaderInterface::class, Twig_Loader_Array::class); |
||
90 | |||
91 | $c->autowire(Twig_Environment::class, Twig_Environment::class) |
||
92 | ->setAutoconfigured(true) |
||
93 | ->setPublic(false); |
||
94 | $c->setAlias(Twig\Environment::class, Twig_Environment::class); |
||
95 | |||
96 | $c->autowire(TwigEngine::class) |
||
97 | ->setAutoconfigured(true) |
||
98 | ->setPublic(false); |
||
99 | $c->setAlias(EngineInterface::class, TwigEngine::class); |
||
100 | |||
101 | if (in_array($this->getEnvironment(), ['test'], true)) { |
||
102 | $c->autowire('test.client', Client::class) |
||
103 | ->setPublic(true); // Public needed! |
||
104 | } |
||
105 | |||
106 | //Controllers |
||
107 | $c->autowire(EngineAsArgumentController::class) |
||
108 | ->setAutoconfigured(true) |
||
109 | ->addTag('controller.service_arguments') |
||
110 | ->setPublic(false); |
||
111 | |||
112 | $this->assertInstanceOf( |
||
113 | // Response::class, // 5.4 < php |
||
114 | 'Symfony\Component\HttpFoundation\Response', |
||
115 | (new HttpKernel( |
||
116 | $dispatcher, |
||
117 | new ContainerControllerResolver($c), |
||
118 | $requestStack, |
||
119 | new ArgumentResolver() |
||
120 | ))->handle(Request::create('/', 'GET')) |
||
121 | ); |
||
122 | } |
||
123 | } |
||
127 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: