Your SlideShare is downloading. ×
Refactorización de aplicaciones PHP/Symfony2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Refactorización de aplicaciones PHP/Symfony2

1,653
views

Published on

Slides de la charla "Refactorización de aplicaciones PHP/Symfony2" en deSymfony

Slides de la charla "Refactorización de aplicaciones PHP/Symfony2" en deSymfony

Published in: Technology

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,653
On Slideshare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
37
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Raúl Fraile• Software developer en• PHP 5.3 Zend Certified Engineer• Symfony Certified Developer• LadybugPHPraulfraile
  • 2. 1. Refactoring 1012. Coding Standard3. IDE4. Separación código/datos5. Acoplamiento al entorno6. Don’t Repeat Yourself7. Fat controllersAgenda
  • 3. Proyecto de prueba
  • 4. APIJokes (I)GET /api/listObtiene la lista de chistes en JSONPOST /api/addAñade un nuevo chistePOST /api/editEdita un chisteGET /Web con la lista de chistes en HTML
  • 5. APIJokes (II)Casos especialesSe envía un email al administradorcada vez que se añade o edita unchiste.No se permiten chistes sobre Java.
  • 6. raulfraile/apijokes
  • 7. Refactoring 101
  • 8. Reescribir VS Refactorizar
  • 9. Reescribirhttp://www.flickr.com/photos/meliah/2601885140/
  • 10. http://www.flickr.com/photos/90692443@N05/8239219385/Refactorizar
  • 11. Refactorizar debe ser unproceso contínuo
  • 12. Siempre partiendo de unsoftware que funciona...
  • 13. ...y con tests
  • 14. Coding Standard
  • 15. http://www.flickr.com/photos/zpeckler/2835570492/¿Cambiar de CS?
  • 16. Importante: Elegir un CSy ser consistente
  • 17. Proyectos open source:compartir mismo CS
  • 18. PSR-1/2
  • 19. http://cs.sensiolabs.org
  • 20. 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 ----------
  • 21. IDE
  • 22. Problemas: DIC, foreach,repositorios...
  • 23. <?php $mailer = $this->get(mailer);$mailer->send($message);
  • 24. <?php /** @var $mailer Swift_Mailer */$mailer = $this->get(mailer);$mailer->send($message);
  • 25. Type hints, aunque nosolo por el IDE
  • 26. <?php $data = array_map(function ($item) {return array(id => $item->getId(),content => $item->getContent());}, $jokes);
  • 27. <?php $data = array_map(function (Joke $item){return array(id => $item->getId(),content => $item->getContent());}, $jokes);
  • 28. Separacióncódigo/datos
  • 29. Son distintoshttp://www.flickr.com/photos/yukariryu/121153772/
  • 30. Un cambio en datos oconfiguración no deberequerir cambiar código
  • 31. 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();}
  • 32. # 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?
  • 33. 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();}
  • 34. Acoplamiento alentorno
  • 35. http://www.flickr.com/photos/darkhornet/4945282009/FAIL
  • 36. {% if app.environment == prod %}<script type="text/javascript">// Google Analytics code</script>{% endif %}
  • 37. La aplicación no debetomar decisionesdependiendo del entorno
  • 38. Mejor: configuracionesdistintas por entorno
  • 39. {% if enable_analytics %}<script type="text/javascript">// Google Analytics code</script>{% endif %}
  • 40. Don’t RepeatYourself
  • 41. // do not allow jokes about java)if (stripos($content, java) !== false) {throw new BadRequestHttpException(Java jokes are not allowed);}
  • 42. use SymfonyComponentValidatorConstraint; /*** @Annotation*/class ContainsJava extends Constraint{public $message = Java jokes are not allowed;}
  • 43.  use SymfonyComponentValidatorConstraint;use SymfonyComponentValidatorConstraintValidator; class ContainsJavaValidator extends ConstraintValidator{public function validate($value, Constraint $constraint){if (stripos($value, java) !== false) {$this->context->addViolation($constraint->message);}}}
  • 44. Fat controllers
  • 45. ¡A dieta!http://www.flickr.com/photos/golf_pictures/3017331832/
  • 46. Configuración, eventos,anotaciones, servicios, métodosmás simples, herencia, paramconverters...
  • 47. https://joind.in/8835¿Preguntas?