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.

Refactorización de aplicaciones PHP/Symfony2

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

  • Be the first to comment

Refactorización de aplicaciones PHP/Symfony2

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

    Be the first to comment

    Login to see the comments

  • xavifuentes

    Jun. 22, 2013
  • jjbier

    Aug. 11, 2013

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

Views

Total views

2,512

On Slideshare

0

From embeds

0

Number of embeds

987

Actions

Downloads

40

Shares

0

Comments

0

Likes

2

×