Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Embracing change

Embracing change

PHP Sount West, 17th June 2015

Jakub Zalas

June 17, 2015
Tweet

More Decks by Jakub Zalas

Other Decks in Programming

Transcript

  1. c l a s s P a c k a

    g e C r a w l e r { p u b l i c f u n c t i o n c r a w l ( ) { $ c h = c u r l _ i n i t ( ) ; c u r l _ s e t o p t ( $ c h , C U R L O P T _ U R L , ' h t t p : / / a c m e . c o m / ' ) ; c u r l _ s e t o p t ( $ c h , C U R L O P T _ R E T U R N T R A N S F E R , 1 ) ; $ r e s p o n s e = c u r l _ e x e c ( $ c h ) ; c u r l _ c l o s e ( $ c h ) ; / / @ t o d o e x t r a c t d e t a i l s f r o m t h e r e s p o n s e . . . } }
  2. SINGLE RESPONSIBILITY PRINCIPLE Gather together the things that change for

    the same reasons. Separate those things that change for different reasons. http://blog.8thlight.com/uncle-bob/2014/05/08/SingleReponsibilityPrinciple.html
  3. COUPLING Modules are coupled if changing one of them requires

    changing another one. Martin Fowler http://martinfowler.com/ieeeSoftware/coupling.pdf
  4. COUPLING THROUGH A PROPERTY u s e B u z

    z \ B r o w s e r ; c l a s s C r a w l e r { / * * * @ v a r B r o w s e r * / p r i v a t e $ b r o w s e r ; }
  5. COUPLING THROUGH A METHOD CALL c l a s s

    C r a w l e r { p r i v a t e $ c ; p u b l i c f u n c t i o n c r a w l ( $ u r l ) { $ t h i s - > c - > g e t B r o w s e r ( ) - > g e t ( $ u r l ) ; } }
  6. COUPLING THROUGH A REFERENCE u s e B u z

    z \ B r o w s e r ; c l a s s C r a w l e r { p u b l i c f u n c t i o n c r a w l ( $ u r l , B r o w s e r $ b ) { $ r e s p o n s e = $ b - > g e t ( $ u r l ) ; } }
  7. COUPLING THROUGH A REFERENCE u s e B u z

    z \ B r o w s e r ; c l a s s C r a w l e r { / * * * @ r e t u r n B r o w s e r * / p u b l i c f u n c t i o n c r a w l ( $ u r l ) { / / . . . r e t u r n $ b r o w s e r ; } }
  8. COUPLING THROUGH AN IMPLEMENTATION / EXTENSION u s e B

    u z z \ B r o w s e r ; c l a s s C r a w l e r e x t e n d s B r o w s e r { p u b l i c f u n c t i o n c r a w l ( $ u r l ) { $ t h i s - > g e t ( $ u r l ) ; } }
  9. INCREASING COHESION u s e B u z z \

    B r o w s e r ; c l a s s P a c k a g e C r a w l e r { p r i v a t e $ b r o w s e r ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( B r o w s e r $ b r o w s e r ) { $ t h i s - > b r o w s e r = $ b r o w s e r ; } p u b l i c f u n c t i o n c r a w l ( ) { $ r e s p o n s e = $ t h i s - > b r o w s e r - > g e t ( ' / ' ) ; / / . . . } }
  10. Count dependencies c l a s s P r o

    d u c t C o n t r o l l e r e x t e n d s C o n t r o l l e r { p u b l i c f u n c t i o n s e a r c h A c t i o n ( $ k e y w o r d s ) { $ p r o d u c t s = $ t h i s - > g e t ( ' d o c t r i n e ' ) - > g e t R e p o s i t o r y ( ' A c m e : P r o d u c t ' ) ; - > s e a r c h ( $ k e y w o r d s ) ; r e t u r n $ t h i s - > r e n d e r ( ' p r o d u c t / s e a r c h . h t m l . t w i g ' , [ ' p r o d u c t s ' = > $ p r o d u c t s ] ) ; } }
  11. HIGH-LEVEL MODULES SHOULD NOT DEPEND ON LOW-LEVEL MODULES. u s

    e B u z z \ B r o w s e r ; c l a s s P a c k a g e C r a w l e r { p r i v a t e $ b r o w s e r ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( B r o w s e r $ b r o w s e r ) { $ t h i s - > b r o w s e r = $ b r o w s e r ; } p u b l i c f u n c t i o n c r a w l ( $ r e s o u r c e ) { $ r e s p o n s e = $ t h i s - > b r o w s e r - > g e t ( $ r e s o u r c e ) ; / / . . . } }
  12. BOTH SHOULD DEPEND ON ABSTRACTIONS. i n t e r

    f a c e H t t p C l i e n t { p u b l i c f u n c t i o n g e t ( $ r e s o u r c e ) ; }
  13. BOTH SHOULD DEPEND ON ABSTRACTIONS. c l a s s

    P a c k a g e C r a w l e r { p r i v a t e $ h t t p C l i e n t ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( H t t p C l i e n t $ h t t p C l i e n t ) { $ t h i s - > h t t p C l i e n t = $ h t t p C l i e n t ; } p u b l i c f u n c t i o n c r a w l ( ) { $ r e s p o n s e = $ t h i s - > h t t p C l i e n t - > g e t ( ' / ' ) ; / / @ t o d o p a r s e } }
  14. BOTH SHOULD DEPEND ON ABSTRACTIONS. u s e B u

    z z \ B r o w s e r ; c l a s s B u z z H t t p C l i e n t i m p l e m e n t s H t t p C l i e n t { p r i v a t e $ b r o w s e r ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( B r o w s e r $ b r o w s e r ) { $ t h i s - > b r o w s e r = $ b r o w s e r ; } p u b l i c f u n c t i o n g e t ( $ r e s o u r c e ) { r e t u r n ( s t r i n g ) $ t h i s - > b r o w s e r - > g e t ( $ r e s o u r c e ) ; } }
  15. i n t e r f a c e C

    o n t e n t P r o v i d e r { p u b l i c f u n c t i o n f e t c h ( $ r e s o u r c e ) ; }
  16. c l a s s P a c k a

    g e C r a w l e r { p r i v a t e $ c o n t e n t P r o v i d e r ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( C o n t e n t P r o v i d e r $ p r o v i d e r ) { $ t h i s - > c o n t e n t P r o v i d e r = $ p r o v i d e r ; } p u b l i c f u n c t i o n c r a w l ( ) { $ c o n t e n t = $ t h i s - > c o n t e n t P r o v i d e r - > f e t c h ( ' / ' ) ; / / @ t o d o p a r s e } }
  17. u s e B u z z \ B r

    o w s e r ; c l a s s B u z z C o n t e n t P r o v i d e r i m p l e m e n t s C o n t e n t P r o v i d e r { p r i v a t e $ b r o w s e r ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( B r o w s e r $ b r o w s e r ) { $ t h i s - > b r o w s e r = $ b r o w s e r ; } p u b l i c f u n c t i o n f e t c h ( ) { r e t u r n ( s t r i n g ) $ t h i s - > b r o w s e r - > g e t ( ' / ' ) ; } }
  18. u s e G u z z l e \

    C l i e n t ; c l a s s G u z z l e C o n t e n t P r o v i d e r i m p l e m e n t s C o n t e n t P r o v i d e r { p r i v a t e $ g u z z l e ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( C l i e n t $ g u z z l e ) { $ t h i s - > g u z z l e = $ g u z z l e ; } p u b l i c f u n c t i o n f e t c h ( $ r e s o u r c e ) { $ r e q u e s t = $ t h i s - > g u z z l e - > c r e a t e R e q u e s t ( ' G E T ' , $ r e s o u r c e ) ; $ r e s p o n s e = $ r e q u e s t - > s e n d ( ) ; r e t u r n $ r e s p o n s e - > g e t B o d y ( ) ; } }
  19. What about the framework? c l a s s P

    r o d u c t C o n t r o l l e r e x t e n d s C o n t r o l l e r { p u b l i c f u n c t i o n s e a r c h A c t i o n ( $ k e y w o r d s ) { $ p r o d u c t s = $ t h i s - > g e t ( ' d o c t r i n e ' ) - > g e t R e p o s i t o r y ( ' A c m e : P r o d u c t ' ) ; - > s e a r c h ( $ k e y w o r d s ) ; r e t u r n $ t h i s - > r e n d e r ( ' p r o d u c t / s e a r c h . h t m l . t w i g ' , [ ' p r o d u c t s ' = > $ p r o d u c t s ] ) ; } }
  20. i n t e r f a c e P

    r o d u c t R e p o s i t o r y { / * * * @ r e t u r n P r o d u c t [ ] * / p u b l i c f u n c t i o n s e a r c h ( $ k e y w o r d s ) ; }
  21. c l a s s D o c t r

    i n e P r o d u c t R e p o s i t o r y i m p l e m e n t s P r o d u c t R e p o s i t o r y { p r i v a t e $ e m ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( E n t i t y M a n a g e r $ e m ) { $ t h i s - > e m = $ e m ; } / * * * @ r e t u r n P r o d u c t [ ] * / p u b l i c f u n c t i o n s e a r c h ( $ k e y w o r d s ) { r e t u r n $ t h i s - > e m - > c r e a t e Q u e r y B u i l d e r ( ) - > s e l e c t ( ' p ' ) - > f r o m ( P r o d u c t : : c l a s s , ' p ' ) - > w h e r e ( ' p . t i t l e L I K E : k e y w o r d s ' ) - > s e t P a r a m e t e r ( ' k e y w o r d s ' , ' % ' . $ k e y w o r d s . ' % ' ) - > g e t Q u e r y ( ) - > g e t R e s u l t ( ) ; } }
  22. c l a s s P r o d u

    c t C o n t r o l l e r { p r i v a t e $ p r o d u c t R e p o s i t o r y ; p r i v a t e $ t e m p l a t i n g ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( P r o d u c t R e p o s i t o r y $ p r o d u c t R e p o s i t o r y , E n g i n e I n t e r f a c e $ t e m p l a t i n g ) { $ t h i s - > p r o d u c t R e p o s i t o r y = $ p r o d u c t R e p o s i t o r y ; $ t h i s - > t e m p l a t i n g = $ t e m p l a t i n g ; } / / . . . }
  23. c l a s s P r o d u

    c t C o n t r o l l e r { / / . . . p u b l i c f u n c t i o n s e a r c h A c t i o n ( $ k e y w o r d s ) { $ p r o d u c t s = $ t h i s - > p r o d u c t R e p o s i t o r y - > s e a r c h ( $ k e y w o r d s ) ; r e t u r n $ t h i s - > t e m p l a t i n g - > r e n d e r R e s p o n s e ( ' p r o d u c t / s e a r c h . h t m l . t w i g ' , [ ' p r o d u c t s ' = > $ p r o d u c t s ] ) ; } }
  24. Good code doesn't take more time to write. What takes

    a lot of time is the learning how to write good code.