some tips to improve developer experience with symfony

34
Some tips to improve developer experience with Symfony by Artem Kolesnikov (@tyomo4ka) 1

Upload: tyomo4ka

Post on 14-Jun-2015

531 views

Category:

Software


0 download

DESCRIPTION

Slides from my talk at PHP User group Minsk (Oct 30 2014)

TRANSCRIPT

Page 1: Some tips to improve developer experience with Symfony

Some tips to improve developer experience

with Symfonyby Artem Kolesnikov (@tyomo4ka)

1

Page 2: Some tips to improve developer experience with Symfony

Common recommendations

2

Page 4: Some tips to improve developer experience with Symfony

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

Page 5: Some tips to improve developer experience with Symfony

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

Page 6: Some tips to improve developer experience with Symfony

Use PHP Storm IDE

• Code analysis

• Great Symfony2 plugin

• Plugins for Behat, composer

• And much, much more

6

Page 7: Some tips to improve developer experience with Symfony

Use PHP syntax sugar and new features

• Short syntax for arrays

• Other syntactic sugar

• Traits

$options = ['yes', 'no'];

$user = (new User())->setEmail('[email protected]');

$headers = $this->getRequest()['headers'];

return $result ?: [];

7

Page 8: Some tips to improve developer experience with Symfony

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

Page 9: Some tips to improve developer experience with Symfony

Symfony specific recommendations

9

Page 10: Some tips to improve developer experience with Symfony

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

Page 11: Some tips to improve developer experience with Symfony

Use one bundle for application

• Recommendation from "Official Symfony Best Practices"

• AppBundle vs App

• use App\Entity\User;

11

Page 12: Some tips to improve developer experience with Symfony

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

Page 13: Some tips to improve developer experience with Symfony

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

Page 14: Some tips to improve developer experience with Symfony

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

Page 15: Some tips to improve developer experience with Symfony

Recommendations from "Official Symfony Best

Practices"

15

Page 16: Some tips to improve developer experience with Symfony

Always use Composer to install Symfony

16

Page 17: Some tips to improve developer experience with Symfony

Configuration management

• Use constants to define configuration options that rarely change

• Don't define a semantic dependency injection configuration for your bundles

17

Page 18: Some tips to improve developer experience with Symfony

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">Metadata\Driver\FileLocator

</parameter>

18

Page 19: Some tips to improve developer experience with Symfony

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

Page 20: Some tips to improve developer experience with Symfony

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

Page 21: Some tips to improve developer experience with Symfony

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

Page 22: Some tips to improve developer experience with Symfony

Some services to improve quality of your projects

22

Page 23: Some tips to improve developer experience with Symfony

Codeship CIhttp://codeship.io

23

Page 24: Some tips to improve developer experience with Symfony

Travis CIhttps://travis-ci.org/

24

Page 25: Some tips to improve developer experience with Symfony

SensioLabs Insighthttps://insight.sensiolabs.com/

25

Page 26: Some tips to improve developer experience with Symfony

Scrutinizer CIhttps://scrutinizer-ci.com

26

Page 27: Some tips to improve developer experience with Symfony

Check new bundles while your drinking your morning coffee

http://knpbundles.com/

27

Check new bundles while your drinking your morning coffee

Page 28: Some tips to improve developer experience with Symfony

And finally my favorite joke about developers :)

28

Page 29: Some tips to improve developer experience with Symfony

The first year

29

Page 30: Some tips to improve developer experience with Symfony

The second year

30

Page 31: Some tips to improve developer experience with Symfony

The third year

31

Page 32: Some tips to improve developer experience with Symfony

The fifth year

32

Page 33: Some tips to improve developer experience with Symfony

The tens year

33

Page 34: Some tips to improve developer experience with Symfony

Thanks!

34