This document provides advice and best practices for writing better code. It discusses topics like improving code quality through testing, refactoring, modularity and design patterns. Specific techniques mentioned include object calisthenics exercises to write simpler code, domain-driven design, and following software development principles like single responsibility and dependency inversion. The overall message is that developers should continuously work to improve their skills and write code that is easy to understand, maintain and extend over time.
22. NIH
| nɒt ɪnˈventɪd hɪə |
Not Invented Here —The German art of humiliating any technology,
agricultural product, or (medical) scientific work that has not been
invented or produced in Germany, Switzerland, or Austria.
source: Urban dictionary
photo by: John 'K'
43. Calisthenics
/ˌkaləsˈTHeniks/
Calisthenics are a form of dynamic exercise consisting of a variety of
simple, often rhythmical, movements, generally using minimal
equipment or apparatus.
photo by: boston_public_library
47. public function createPost($request)
{
$entity = new Post();
$form = new MyForm($entity);
$form->bind($request);
if ($form->isValid()){
$repository = $this->getRepository('MyBundle:Post');
if (!$repository->exists($entity) ) {
$repository->save($entity);
return $this->redirect('create_ok');
} else {
$error = "Post Title already exists";
return array('form' => $form, 'error' => $error);
}
} else {
$error = "Invalid fields";
return array('form' => $form, 'error' => $error);
}
}
48. public function createPost($request)
{
$entity = new Post();
$form = new MyForm($entity);
$form->bind($request);
if ($form->isValid()){
$repository = $this->getRepository('MyBundle:Post');
if (!$repository->exists($entity) ) {
$repository->save($entity);
return $this->redirect('create_ok');
} else {
$error = "Post Title already exists";
return array('form' => $form, 'error' => $error);
}
} else {
$error = "Invalid fields";
return array('form' => $form, 'error' => $error);
}
}
actual goal of function
“Create Post"
49. public function createPost($request)
{
$entity = new Post();
$form = new MyForm($entity);
$form->bind($request);
if ($form->isValid()){
$repository = $this->getRepository('MyBundle:Post');
if (!$repository->exists($entity) ) {
$repository->save($entity);
return $this->redirect('create_ok');
} else {
$error = "Post Title already exists";
return array('form' => $form, 'error' => $error);
}
} else {
$error = "Invalid fields";
return array('form' => $form, 'error' => $error);
}
}
this is all
error
handling
50. public function createPost($request)
{
$entity = new Post();
$form = new MyForm($entity);
$form->bind($request);
if ($form->isValid()){
$repository = $this->getRepository('MyBundle:Post');
if (!$repository->exists($entity) ) {
$repository->save($entity);
return $this->redirect('create_ok');
} else {
$error = "Post Title already exists";
return array('form' => $form, 'error' => $error);
}
} else {
$error = "Invalid fields";
return array('form' => $form, 'error' => $error);
}
}
51. public function createPost($request)
{
$entity = new Post();
$repository = $this->getRepository('MyBundle:Post');
$form = new MyForm($entity);
$form->bind($request);
if ($form->isValid() === false){
return array('form' => $form, 'error' => 'Invalid fields');
}
if ($repository->exists($entity)){
return array('form' => $form, 'error' => 'Duplicate post
title');
}
$repository->save($entity);
return $this->redirect('create_ok');
}
52. public function createPost($request)
{
$entity = new Post();
$repository = $this->getRepository('MyBundle:Post');
$form = new MyForm($entity);
$form->bind($request);
if ($form->isValid() === false){
return array('form' => $form, 'error' => 'Invalid fields');
}
if ($repository->exists($entity)){
return array('form' => $form, 'error' => 'Duplicate post
title');
}
$repository->save($entity);
return $this->redirect('create_ok');
}
exit condition 1: invalid form
53. public function createPost($request)
{
$entity = new Post();
$repository = $this->getRepository('MyBundle:Post');
$form = new MyForm($entity);
$form->bind($request);
if ($form->isValid() === false){
return array('form' => $form, 'error' => 'Invalid fields');
}
if ($repository->exists($entity)){
return array('form' => $form, 'error' => 'Duplicate post
title');
}
$repository->save($entity);
return $this->redirect('create_ok');
}
exit condition 2: duplicated post
54. public function createPost($request)
{
$entity = new Post();
$repository = $this->getRepository('MyBundle:Post');
$form = new MyForm($entity);
$form->bind($request);
if ($form->isValid() === false){
return array('form' => $form, 'error' => 'Invalid fields');
}
if ($repository->exists($entity)){
return array('form' => $form, 'error' => 'Duplicate post
title');
}
$repository->save($entity);
return $this->redirect('create_ok');
}
this is what the
method actually
does
56. public function createPost($request)
{
$repository = $this->getRepository('MyBundle:Post');
$entity = new Post();
$repository->save($entity);
return $this->redirect('create_ok');
}
FormValidationMiddleware
UniqueEntityMiddleware
57. public function createPost($request)
{
$repository = $this->getRepository('MyBundle:Post');
$entity = new Post();
$repository->save($entity);
return $this->redirect('create_ok');
}
The power of PSR-7
and middleware
FormValidationMiddleware
UniqueEntityMiddleware
82. Be part of this community!
Embrace open source, and
add to it!
83. DDD, Auto-wire, contribute to
PHP, write secure code, use flex,
use docker, do static analysis,
phpStan CQRS, use fixtures, be
strict, communicate, document,
collect elephpants… and all the
other amazing topics you saw
this weekend.
#phpce18
84. DDD, Auto-wire, contribute to
PHP, write secure code, use flex,
use docker, do static analysis,
phpStan CQRS, use fixtures, be
strict, communicate, document,
collect elephpants… and all the
other amazing topics you saw
this weekend.
#phpce18