SlideShare ist ein Scribd-Unternehmen logo
1 von 34
Downloaden Sie, um offline zu lesen
Some tips to improve 
developer experience 
with Symfony 
by Artem Kolesnikov (@tyomo4ka) 
1
Common 
recommendations 
2
Code style 
• Symfony uses PSR-1 and PSR-2 
• Symfony extends PSRs with Symfony Coding 
Standards 
• PHP CS Fixer supports PSRs and Symfony 
• Broken windows theory 
3
Prefer clean naming to 
comments 
• "Every time you write a comment, you should 
grimace and feel the failure of your ability of 
expression." © Robert C. Martin "Clean code" 
• Always comment complex and not obvious logic 
• Remove obsolete comments 
• Never commit commented unused code 
4
Avoid business logic in 
controllers 
• Fat stupid ugly controllers (FSUC) 
// FSUC 
public function updateUserAction(User $user, $userData) 
{ 
$user->updateFromArray($userData); 
$this->getDoctrine()->getManager()->flush($user); 
} 
// OK 
public function updateUserAction(User $user, $userData) 
{ 
$this->get('user_manager')->update($user, $userData); 
} 
5
Use PHP Storm IDE 
• Code analysis 
• Great Symfony2 plugin 
• Plugins for Behat, composer 
• And much, much more 
6
Use PHP syntax sugar and 
new features 
• Short syntax for arrays 
• Other syntactic sugar 
• Traits 
$options = ['yes', 'no']; 
$user = (new User())->setEmail('tyomo4ka@gmail.com'); 
$headers = $this->getRequest()['headers']; 
return $result ?: []; 
7
Don't use interfaces when 
you don't need it 
• Dependency on abstract class works as well 
• Introduce interface is much simpler than remove 
• Interfaces for everything... 
8
Symfony specific 
recommendations 
9
Don't inject service container 
in business logic services 
/** Service which does not depend on Container */ 
class UserManager 
{ 
private $em; 
public function __construct(EntityManager $em) 
{ 
$this->em = $em; 
} 
} 
/** Service which depends on Container */ 
class UserManager 
{ 
private $container; 
public function __construct(ContainerInterface $container) 
{ 
$this->container = $container; 
} 
} 
10
Use one bundle for 
application 
• Recommendation from 
"Official Symfony Best 
Practices" 
• AppBundle vs App 
• use AppEntityUser; 
11
Always use param 
converters 
• Param convert works using request attributes 
public function someAction(ContextEntity $entity) {} 
• Don't write action like this 
public function deleteTagAction() 
{ 
$this->get('xxx.tag_manager')->deleteTagFromStream( 
$this->getRequest()->get('stream_id'), 
$this->getRequest()->get('tag_id') 
); 
} 
• This looks much cleaner 
public function deleteTagAction(Stream $stream, Tag $tag) 
{ 
$this->get('xxx.tag_manager') 
->deleteTagFromStream($stream, $tag) 
; 
} 
12
Prefer security voters to ACL 
• Each ACL check requires requests to DB 
• Voters are simple 
interface VoterInterface 
{ 
function supportsAttribute($attribute); 
function supportsClass($class); 
function vote(TokenInterface $token, $object, array $attributes); 
} 
• Abstract voter (Symfony 2.6+) 
abstract class AbstractVoter implements VoterInterface 
{ 
abstract protected function getSupportedClasses(); 
abstract protected function getSupportedAttributes(); 
abstract protected function isGranted($attribute, $object, $user = null); 
} 
13
Prefer Grunt or Gulp to Assetic 
if you have reach front-end 
• Assetic is good if you don't have too much 
assets 
• Cool like a Frontend Developer: Grunt, 
RequireJS, Bower and other Tools by Ryan 
Weaver (slides and video) 
14
Recommendations from 
"Official Symfony Best 
Practices" 
15
Always use Composer 
to install Symfony 
16
Configuration management 
• Use constants to define configuration options 
that rarely change 
• Don't define a semantic dependency injection 
configuration for your bundles 
17
Services definitions 
• The name of your application's services should be 
as short as possible, ideally just one simple word 
• Use the YAML format to define your own services 
• Don't define parameters for the classes of your 
services 
<parameter key="jms_serializer.metadata.file_locator.class"> 
MetadataDriverFileLocator 
</parameter> 
18
Configuration format 
• Use annotations to define the mapping 
information of the Doctrine entities 
• Use annotations to configure routing, caching 
and security whenever possible 
• Don't use the @Template() annotation to 
configure the template used by the controller 
19
Templating 
• Use Twig templating format for your templates 
• PHP templating engine will be removed in the 
future 
• Store all your application's templates in app/ 
Resources/views/ directory 
20
Some other official recommendations 
• Define your forms as PHP classes 
• Always use keys for translations instead of 
content strings 
{{ 'project_name' |trans }} // Good 
{{ 'Project name' |trans }} // Not good 
21
Some services to improve 
quality of your projects 
22
Codeship CI 
http://codeship.io 
23
Travis CI 
https://travis-ci.org/ 
24
SensioLabs Insight 
https://insight.sensiolabs.com/ 
25
Scrutinizer CI 
https://scrutinizer-ci.com 
26
Check new bundles while 
your drinking your morning coffee 
Check new bundles while your drinking 
your morning coffee 
http://knpbundles.com/ 
27
And finally my favorite 
joke about developers :) 
28
The first year 
29
The second year 
30
The third year 
31
The fifth year 
32
The tens year 
33
Thanks! 
34

Weitere ähnliche Inhalte

Was ist angesagt?

Working with oro crm entities
Working with oro crm entitiesWorking with oro crm entities
Working with oro crm entities
Oro Inc.
 

Was ist angesagt? (20)

Hello World on Slim Framework 3.x
Hello World on Slim Framework 3.xHello World on Slim Framework 3.x
Hello World on Slim Framework 3.x
 
Elefrant [ng-Poznan]
Elefrant [ng-Poznan]Elefrant [ng-Poznan]
Elefrant [ng-Poznan]
 
Working with oro crm entities
Working with oro crm entitiesWorking with oro crm entities
Working with oro crm entities
 
Rack
RackRack
Rack
 
Laravel 5 In Depth
Laravel 5 In DepthLaravel 5 In Depth
Laravel 5 In Depth
 
Phoenix demysitify, with fun
Phoenix demysitify, with funPhoenix demysitify, with fun
Phoenix demysitify, with fun
 
Web services with laravel
Web services with laravelWeb services with laravel
Web services with laravel
 
Javascript laravel's friend
Javascript laravel's friendJavascript laravel's friend
Javascript laravel's friend
 
Resting with OroCRM Webinar
Resting with OroCRM WebinarResting with OroCRM Webinar
Resting with OroCRM Webinar
 
Adventurous Merb
Adventurous MerbAdventurous Merb
Adventurous Merb
 
Laravel Design Patterns
Laravel Design PatternsLaravel Design Patterns
Laravel Design Patterns
 
Lecture6
Lecture6Lecture6
Lecture6
 
OroCRM Partner Technical Training: September 2015
OroCRM Partner Technical Training: September 2015OroCRM Partner Technical Training: September 2015
OroCRM Partner Technical Training: September 2015
 
REST APIs in Laravel 101
REST APIs in Laravel 101REST APIs in Laravel 101
REST APIs in Laravel 101
 
Oro Workflows
Oro WorkflowsOro Workflows
Oro Workflows
 
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra  SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
 
Cocoa on-rails-3rd
Cocoa on-rails-3rdCocoa on-rails-3rd
Cocoa on-rails-3rd
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
 
Workshop 3: JavaScript build tools
Workshop 3: JavaScript build toolsWorkshop 3: JavaScript build tools
Workshop 3: JavaScript build tools
 
Magento - a Zend Framework Application
Magento - a Zend Framework ApplicationMagento - a Zend Framework Application
Magento - a Zend Framework Application
 

Ähnlich wie Some tips to improve developer experience with Symfony

Speed up your developments with Symfony2
Speed up your developments with Symfony2Speed up your developments with Symfony2
Speed up your developments with Symfony2
Hugo Hamon
 

Ähnlich wie Some tips to improve developer experience with Symfony (20)

Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenches
 
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
 
What's New In Laravel 5
What's New In Laravel 5What's New In Laravel 5
What's New In Laravel 5
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the Trenches
 
Red5 - PHUG Workshops
Red5 - PHUG WorkshopsRed5 - PHUG Workshops
Red5 - PHUG Workshops
 
Working With The Symfony Admin Generator
Working With The Symfony Admin GeneratorWorking With The Symfony Admin Generator
Working With The Symfony Admin Generator
 
The Naked Bundle - Tryout
The Naked Bundle - TryoutThe Naked Bundle - Tryout
The Naked Bundle - Tryout
 
AD102 - Break out of the Box
AD102 - Break out of the BoxAD102 - Break out of the Box
AD102 - Break out of the Box
 
Speed up your developments with Symfony2
Speed up your developments with Symfony2Speed up your developments with Symfony2
Speed up your developments with Symfony2
 
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
 
Developing Lightning Components for Communities.pptx
Developing Lightning Components for Communities.pptxDeveloping Lightning Components for Communities.pptx
Developing Lightning Components for Communities.pptx
 
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...
 
Creating a modern web application using Symfony API Platform, ReactJS and Red...
Creating a modern web application using Symfony API Platform, ReactJS and Red...Creating a modern web application using Symfony API Platform, ReactJS and Red...
Creating a modern web application using Symfony API Platform, ReactJS and Red...
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
 
Simplify your professional web development with symfony
Simplify your professional web development with symfonySimplify your professional web development with symfony
Simplify your professional web development with symfony
 
6 tips for improving ruby performance
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performance
 
The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014
 
Hexagonal architecture
Hexagonal architectureHexagonal architecture
Hexagonal architecture
 
Overview of MVC Framework - by software outsourcing company india
Overview of MVC Framework - by software outsourcing company indiaOverview of MVC Framework - by software outsourcing company india
Overview of MVC Framework - by software outsourcing company india
 

Kürzlich hochgeladen

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 

Kürzlich hochgeladen (20)

OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 

Some tips to improve developer experience with Symfony

  • 1. Some tips to improve developer experience with Symfony by Artem Kolesnikov (@tyomo4ka) 1
  • 3. Code style • Symfony uses PSR-1 and PSR-2 • Symfony extends PSRs with Symfony Coding Standards • PHP CS Fixer supports PSRs and Symfony • Broken windows theory 3
  • 4. Prefer clean naming to comments • "Every time you write a comment, you should grimace and feel the failure of your ability of expression." © Robert C. Martin "Clean code" • Always comment complex and not obvious logic • Remove obsolete comments • Never commit commented unused code 4
  • 5. Avoid business logic in controllers • Fat stupid ugly controllers (FSUC) // FSUC public function updateUserAction(User $user, $userData) { $user->updateFromArray($userData); $this->getDoctrine()->getManager()->flush($user); } // OK public function updateUserAction(User $user, $userData) { $this->get('user_manager')->update($user, $userData); } 5
  • 6. Use PHP Storm IDE • Code analysis • Great Symfony2 plugin • Plugins for Behat, composer • And much, much more 6
  • 7. Use PHP syntax sugar and new features • Short syntax for arrays • Other syntactic sugar • Traits $options = ['yes', 'no']; $user = (new User())->setEmail('tyomo4ka@gmail.com'); $headers = $this->getRequest()['headers']; return $result ?: []; 7
  • 8. Don't use interfaces when you don't need it • Dependency on abstract class works as well • Introduce interface is much simpler than remove • Interfaces for everything... 8
  • 10. Don't inject service container in business logic services /** Service which does not depend on Container */ class UserManager { private $em; public function __construct(EntityManager $em) { $this->em = $em; } } /** Service which depends on Container */ class UserManager { private $container; public function __construct(ContainerInterface $container) { $this->container = $container; } } 10
  • 11. Use one bundle for application • Recommendation from "Official Symfony Best Practices" • AppBundle vs App • use AppEntityUser; 11
  • 12. Always use param converters • Param convert works using request attributes public function someAction(ContextEntity $entity) {} • Don't write action like this public function deleteTagAction() { $this->get('xxx.tag_manager')->deleteTagFromStream( $this->getRequest()->get('stream_id'), $this->getRequest()->get('tag_id') ); } • This looks much cleaner public function deleteTagAction(Stream $stream, Tag $tag) { $this->get('xxx.tag_manager') ->deleteTagFromStream($stream, $tag) ; } 12
  • 13. Prefer security voters to ACL • Each ACL check requires requests to DB • Voters are simple interface VoterInterface { function supportsAttribute($attribute); function supportsClass($class); function vote(TokenInterface $token, $object, array $attributes); } • Abstract voter (Symfony 2.6+) abstract class AbstractVoter implements VoterInterface { abstract protected function getSupportedClasses(); abstract protected function getSupportedAttributes(); abstract protected function isGranted($attribute, $object, $user = null); } 13
  • 14. Prefer Grunt or Gulp to Assetic if you have reach front-end • Assetic is good if you don't have too much assets • Cool like a Frontend Developer: Grunt, RequireJS, Bower and other Tools by Ryan Weaver (slides and video) 14
  • 15. Recommendations from "Official Symfony Best Practices" 15
  • 16. Always use Composer to install Symfony 16
  • 17. Configuration management • Use constants to define configuration options that rarely change • Don't define a semantic dependency injection configuration for your bundles 17
  • 18. Services definitions • The name of your application's services should be as short as possible, ideally just one simple word • Use the YAML format to define your own services • Don't define parameters for the classes of your services <parameter key="jms_serializer.metadata.file_locator.class"> MetadataDriverFileLocator </parameter> 18
  • 19. Configuration format • Use annotations to define the mapping information of the Doctrine entities • Use annotations to configure routing, caching and security whenever possible • Don't use the @Template() annotation to configure the template used by the controller 19
  • 20. Templating • Use Twig templating format for your templates • PHP templating engine will be removed in the future • Store all your application's templates in app/ Resources/views/ directory 20
  • 21. Some other official recommendations • Define your forms as PHP classes • Always use keys for translations instead of content strings {{ 'project_name' |trans }} // Good {{ 'Project name' |trans }} // Not good 21
  • 22. Some services to improve quality of your projects 22
  • 27. Check new bundles while your drinking your morning coffee Check new bundles while your drinking your morning coffee http://knpbundles.com/ 27
  • 28. And finally my favorite joke about developers :) 28