Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
REFACTORINGPHP/SYMFONY2APPS
Thank you to all our sponsors!
Raúl Fraile• Software developer at• PHP 5.3 Zend Certified Engineer• Symfony Certified Developer• LadybugPHPraulfraile
1. Refactoring 1012. Coding Standard3. IDE4. Code/Data separation5. Environment coupling6. Don’t Repeat Yourself7. Fat con...
Test project
APIJokes (I)GET /api/listGet the jokes list in JSONPOST /api/addAdd a new jokePOST /api/editEdit an existing jokeGET /Publ...
APIJokes (II)Special casesEvery time a joke is added or edited,an email is sent to the administrator.We don’t allow jokes ...
raulfraile/apijokesThere are tags for each step!
Refactoring 101
Rewrite VS Refactor
Rewritehttp://www.flickr.com/photos/meliah/2601885140/
http://www.flickr.com/photos/90692443@N05/8239219385/Refactor
Refactoring is acontinuous process
Always taking a softwarethat works...
...and which has tests
Coding Standard
http://www.flickr.com/photos/zpeckler/2835570492/Change my CS?!
Important: Choose a CSand be consistent
Open source projects:share the same CS
PSR-1/2
http://cs.sensiolabs.org
php-cs-fixer fix ApiJokesBundle/ --level=all --dry-run --diff -v1) Controller/WebsiteController.php (braces, return)------...
IDE
Problems: DIC, foreach,repositories...
<?php $mailer = $this->get(mailer);$mailer->send($message);
<?php /** @var $mailer Swift_Mailer */$mailer = $this->get(mailer);$mailer->send($message);
Type hints, not only tohelp the IDE...
<?php $data = array_map(function ($item) {return array(id => $item->getId(),content => $item->getContent());}, $jokes);
<?php $data = array_map(function (Joke $item){return array(id => $item->getId(),content => $item->getContent());}, $jokes);
Code/Dataseparation
They are differenthttp://www.flickr.com/photos/yukariryu/121153772/
A change in the data orconfiguration should’trequire changing the code
public function load(ObjectManager $manager){$jokes = array(There’s no place like 127.0.0.1,If at first you don’t succeed;...
# fixtures/joke.ymljokes:- I would love to change the world, but they won’t...- There’s no place like 127.0.0.1- If at fir...
public function load(ObjectManager $manager){$jokes = Yaml::parse(__DIR__ . /fixtures/joke.yml); foreach ($jokes[jokes] as...
Environment coupling
http://www.flickr.com/photos/darkhornet/4945282009/FAIL
{% if app.environment == prod %}<script type="text/javascript">// Google Analytics code</script>{% endif %}
The software shouldn’ttake any decision basedon the environment
Better approach:different configurationfor each environment
{% if enable_analytics %}<script type="text/javascript">// Google Analytics code</script>{% endif %}
Don’t RepeatYourself
// do not allow jokes about java)if (stripos($content, java) !== false) {throw new BadRequestHttpException(Java jokes are ...
use SymfonyComponentValidatorConstraint; /*** @Annotation*/class ContainsJava extends Constraint{public $message = Java jo...
 use SymfonyComponentValidatorConstraint;use SymfonyComponentValidatorConstraintValidator; class ContainsJavaValidator ext...
Fat controllers
Go on a diet!http://www.flickr.com/photos/golf_pictures/3017331832/
Configuration, events,annotations, services, simplermethods, inheritance, paramconverters...
https://joind.in/8835Questions?
Refactoring PHP/Symfony2 apps
Refactoring PHP/Symfony2 apps
Upcoming SlideShare
Loading in …5
×

Refactoring PHP/Symfony2 apps

7,094 views

Published on

Slides for deSymfony 2013

  • Be the first to comment

Refactoring PHP/Symfony2 apps

  1. 1. REFACTORINGPHP/SYMFONY2APPS
  2. 2. Thank you to all our sponsors!
  3. 3. Raúl Fraile• Software developer at• PHP 5.3 Zend Certified Engineer• Symfony Certified Developer• LadybugPHPraulfraile
  4. 4. 1. Refactoring 1012. Coding Standard3. IDE4. Code/Data separation5. Environment coupling6. Don’t Repeat Yourself7. Fat controllersAgenda
  5. 5. Test project
  6. 6. APIJokes (I)GET /api/listGet the jokes list in JSONPOST /api/addAdd a new jokePOST /api/editEdit an existing jokeGET /Public website with the joke list in HTML
  7. 7. APIJokes (II)Special casesEvery time a joke is added or edited,an email is sent to the administrator.We don’t allow jokes about Java.
  8. 8. raulfraile/apijokesThere are tags for each step!
  9. 9. Refactoring 101
  10. 10. Rewrite VS Refactor
  11. 11. Rewritehttp://www.flickr.com/photos/meliah/2601885140/
  12. 12. http://www.flickr.com/photos/90692443@N05/8239219385/Refactor
  13. 13. Refactoring is acontinuous process
  14. 14. Always taking a softwarethat works...
  15. 15. ...and which has tests
  16. 16. Coding Standard
  17. 17. http://www.flickr.com/photos/zpeckler/2835570492/Change my CS?!
  18. 18. Important: Choose a CSand be consistent
  19. 19. Open source projects:share the same CS
  20. 20. PSR-1/2
  21. 21. http://cs.sensiolabs.org
  22. 22. php-cs-fixer fix ApiJokesBundle/ --level=all --dry-run --diff -v1) Controller/WebsiteController.php (braces, return)---------- begin diff ------------- Original+++ New@@ @@-class WebsiteController extends Controller {- public function indexAction() {+class WebsiteController extends Controller+{+ public function indexAction()+ {$em = $this->getDoctrine()->getManager();$jokes = $em->getRepository(...)->findAll();+return $this->render(..., array(jokes => $jokes));}}---------- end diff ----------
  23. 23. IDE
  24. 24. Problems: DIC, foreach,repositories...
  25. 25. <?php $mailer = $this->get(mailer);$mailer->send($message);
  26. 26. <?php /** @var $mailer Swift_Mailer */$mailer = $this->get(mailer);$mailer->send($message);
  27. 27. Type hints, not only tohelp the IDE...
  28. 28. <?php $data = array_map(function ($item) {return array(id => $item->getId(),content => $item->getContent());}, $jokes);
  29. 29. <?php $data = array_map(function (Joke $item){return array(id => $item->getId(),content => $item->getContent());}, $jokes);
  30. 30. Code/Dataseparation
  31. 31. They are differenthttp://www.flickr.com/photos/yukariryu/121153772/
  32. 32. A change in the data orconfiguration should’trequire changing the code
  33. 33. public function load(ObjectManager $manager){$jokes = array(There’s no place like 127.0.0.1,If at first you don’t succeed; call it version 1.0,Beware of programmers that carry screwdrivers,What color do you want that database?); foreach ($jokes as $item) {$joke = new Joke();$joke->setContent($item); $manager->persist($joke);} $manager->flush();}
  34. 34. # fixtures/joke.ymljokes:- I would love to change the world, but they won’t...- There’s no place like 127.0.0.1- If at first you don’t succeed; call it version 1.0- You know it’s love when you memorize her IP...- Beware of programmers that carry screwdrivers- Best file compression around: “rm *.*” = 100...- The truth is out there…anybody got the URL?- What color do you want that database?
  35. 35. public function load(ObjectManager $manager){$jokes = Yaml::parse(__DIR__ . /fixtures/joke.yml); foreach ($jokes[jokes] as $item) {$joke = new Joke();$joke->setContent($item); $manager->persist($joke);} $manager->flush();}
  36. 36. Environment coupling
  37. 37. http://www.flickr.com/photos/darkhornet/4945282009/FAIL
  38. 38. {% if app.environment == prod %}<script type="text/javascript">// Google Analytics code</script>{% endif %}
  39. 39. The software shouldn’ttake any decision basedon the environment
  40. 40. Better approach:different configurationfor each environment
  41. 41. {% if enable_analytics %}<script type="text/javascript">// Google Analytics code</script>{% endif %}
  42. 42. Don’t RepeatYourself
  43. 43. // do not allow jokes about java)if (stripos($content, java) !== false) {throw new BadRequestHttpException(Java jokes are not allowed);}
  44. 44. use SymfonyComponentValidatorConstraint; /*** @Annotation*/class ContainsJava extends Constraint{public $message = Java jokes are not allowed;}
  45. 45.  use SymfonyComponentValidatorConstraint;use SymfonyComponentValidatorConstraintValidator; class ContainsJavaValidator extends ConstraintValidator{public function validate($value, Constraint $constraint){if (stripos($value, java) !== false) {$this->context->addViolation($constraint->message);}}}
  46. 46. Fat controllers
  47. 47. Go on a diet!http://www.flickr.com/photos/golf_pictures/3017331832/
  48. 48. Configuration, events,annotations, services, simplermethods, inheritance, paramconverters...
  49. 49. https://joind.in/8835Questions?

×