When PHP Framework Sucks Series: Magic inside frameworks
Tags: php, webdev, career, programming
Published: 2018-12-24 14:43:36

---
title: When PHP Framework Sucks Series: Magic inside frameworks
published: true
series: When PHP Framework Sucks
description: A framework magic can be evil and work against you
tags: php, webdev, career, programming
cover_image: https://thepracticaldev.s3.amazonaws.com/i/eycwqg7yhsop6oe37ek1.jpg
---
From all of the topics, I covered at my "When PHP Framework Sucks" series, this one maybe annoys me the most. Not because I have a particular problem with style or standards for annotations and configurations than because I'm unable to reuse code and expect the same behavior with any other environment.
What I meant when I say "framework magic"? I prefer to complain about these four magic points:
1. Defining code inside method or class annotations
2. Defining classes instances in yaml or any file different than .php
3. All config inside yaml, XML, or any other file
4. Containers which "contains" anything

####Defining code inside method or class annotations
We tend to use annotations so often for defining method properties. I recently saw complete documentation for swagger in controller above every action. My god, it was so long that I wasn't able to fit it in my screen height. The notation which exceeds screen hight! That's a red alarm of first grade. Long notations are not just ugly, they create so much noise, that prevents you from seeing the actual code.
It is also very common to see validation inside notation. I disagree with that so much. First, if you change the library for validation, reuse your code somewhere else, validation library becomes deprecated or similar, you will end up with the useless string above your method.
Instead, use some Value Object for validating specific data types, try to enforce strong types in every method.  
```php
/**
 *@library I‘m the useless piece of comment if you change the library hehehehe
 */
public function someMethod()
{
}
```
####Defining classes instances in yaml or any file different than .php
Classes instances in non-PHP files are very problematic if you want to follow the execution line in your IDE and then you end up that some class is a dead end. This class is not a dead end as it seems to be, there is a code which instantiates it, but it is in yaml. 
Wait, YAML ???
True, many time I see the declaration for classes which we have to define as a "service" for example, inside XML or yaml files.
####All config inside yaml, XML, or any other file
Generally, config inside yaml files annoys me. I know it brings some readability for folks, but I always wonder, why not simply in php? No need to parse, no need to warm up.
####Containers which "contains" anything
This one is wrong, at least from two perspectives. Perspective number one, the container return type is what? Containers can return on get method call (or similar name) any type, any object, or property. How do I know that in the name of God? How can I call any method on that object since I don't know its type?
```php
public function testAction()
{
    $something = $this->container->get('hehehehe_you_newer_know_who_I_am');
}
```
The second problem is that container violates such a hard "Interface segregation principle" the famous "I" in SOLID principles. Your Controller or any other class which has access to the container knows too many things at this point. 
####Conclusion
Notations are comments, you can use it to specify some method properties, but do it with caution. If you try to find the usage of the class, defined in yaml, good luck! There is unnecessary parsing of yaml and XML configs. Controllers and other container aware objects know too much, but you don't have an idea what container returns.