PHP para Adultos: Clean Code e Object Calisthenics

download PHP para Adultos: Clean Code e Object Calisthenics

of 90

  • date post

    27-Aug-2014
  • Category

    Software

  • view

    1.498
  • download

    12

Embed Size (px)

description

Palestra apresentada no InterconPHP em São Paulo. Data do evento: 2014-07-19

Transcript of PHP para Adultos: Clean Code e Object Calisthenics

  • TitleText Guilherme Blanco PHP4Adults Object Calisthenics Clean Code
  • Guilherme Blanco PHP4Adults Who speaks English?
  • Guilherme Blanco PHP4Adults "Vamos l, Sucker! Mexa esse traseiro gordo!" Fucker & Sucker: A Pair of Two Double Cops
  • Guilherme Blanco PHP4Adults Guilherme Blanco @guilhermeblanco github.com/guilhermeblanco
  • Guilherme Blanco PHP4Adults Instaclick ~300 empregados Mais de 67 milhes de usurios (somente em um website) 12000 requisies/segundo Ns usamos PHP!
  • Guilherme Blanco PHP4Adults Motivao Legibilidade Mantenabilidade Reusabilidade Testabilidade
  • Guilherme Blanco PHP4Adults Resumindo
  • Guilherme Blanco PHP4Adults
  • Guilherme Blanco PHP4Adults Clean Code
  • Guilherme Blanco PHP4Adults S T U P I Dingleton ightcoupling ntestability rematureoptimization ndescriptivenaming uplication
  • Guilherme Blanco PHP4Adults S O L I D ingleresponsibility pen/closeprinciple iskovsubstitution nterfacesegregation ependencyinversion
  • Guilherme Blanco PHP4Adults Liskov Substitution Principle
  • Guilherme Blanco PHP4Adults ! interface Bird! {! public function setLocation($longitude, $latitude);! ! public function setAltitude($altitude);! ! public function draw();! }!
  • Guilherme Blanco PHP4Adults ! class Penguim implements Bird! {! public function setAltitude($altitude)! {! // Do nothing! }! }!
  • Guilherme Blanco PHP4Adults ! interface Bird! {! public function setLocation($longitude, $latitude);! ! public function draw($altitude);! }! ! interface FlightfulBird extends Bird! {! ! public function setAltitude($altitude);! }!
  • Guilherme Blanco PHP4Adults Dependency Inversion Principle
  • Guilherme Blanco PHP4Adults ! namespace DatingUserBundleEntity! {! class User! {! /**! * @var DatingUserBundleEntityImage! */! ! protected $avatar;! }! }! ! namespace DatingMediaBundleEntity! {! class Image! {! /**! * @var DatingUserBundleEntityUser! */! ! protected $owner;! ! }! }
  • Guilherme Blanco PHP4Adults ! namespace DatingUserBundleEntity! {! class User! {! /**! * @var AvatarInterface! */! ! protected $avatar;! }! ! interface AvatarInterface! {! // ...! }! }! ! namespace DatingMediaBundleEntity! {! use DatingUserBundleEntityAvatarInterface;! ! class Image implements AvatarInterface! {! /**! * @var DatingUserBundleEntityUser! */! ! protected $owner;! ! }! }!
  • Guilherme Blanco PHP4Adults Object Calisthenics Termo grego, significa "exerccio"
  • Guilherme Blanco PHP4Adults Somente um nvel de indentao por mtodo
  • Guilherme Blanco PHP4Adults ! public function validateForm($filters='', $validators='', $options='')! { ! $data = $_POST; ! ! $input = new Zend_Filter_Input($filters, $validators, $data, $options); ! $input->setDefaultEscapeFilter(new Zend_Filter_StringTrim()); ! ! if ($input->hasInvalid() || $input->hasMissing()) {! foreach ($input->getMessages() as $field => $messageList) {! foreach ($messageList as $message) {! if (strpos($message, "empty")) {! throw new Tss_FormException(! "The field {$field} cannot be empty!", ! 3, ! "javascript:history.back();"! );! } else { ! throw new Tss_FormException(! "{$message}", ! 3, ! "javascript:history.back();"! );! }! }! }! }! ! return $input;! }
  • Guilherme Blanco PHP4Adults ! public function validateForm($filters='', $validators='', $options='')! { ! $data = $_POST; ! ! $input = new Zend_Filter_Input($filters, $validators, $data, $options); ! $input->setDefaultEscapeFilter(new Zend_Filter_StringTrim()); ! ! if ($input->hasInvalid() || $input->hasMissing()) {! foreach ($input->getMessages() as $field => $messageList) {! foreach ($messageList as $message) {! if (strpos($message, "empty")) {! throw new Tss_FormException(! "The field {$field} cannot be empty!", ! 3, ! "javascript:history.back();"! );! } else { ! throw new Tss_FormException(! "{$message}", ! 3, ! "javascript:history.back();"! );! }! }! }! }! ! return $input;! } 0 Class prototype 1 2 3 4
  • Guilherme Blanco PHP4Adults Early returns
  • Guilherme Blanco PHP4Adults ! public function validateForm($filters=array(), $validators=array(), $options=null)! { ! $data = $_POST; ! ! $input = new Zend_Filter_Input($filters, $validators, $data, $options); ! $input->setDefaultEscapeFilter(new Zend_Filter_StringTrim()); ! ! if ( ! ($input->hasInvalid() || $input->hasMissing())) {! ! return $input;! ! }! ! foreach ($input->getMessages() as $field => $messageList) {! foreach ($messageList as $message) {! if (strpos($message, "empty")) {! throw new Tss_FormException(! "The field {$field} cannot be empty!", ! 3, ! "javascript:history.back();"! );! } else { ! throw new Tss_FormException(! "{$message}", ! 3, ! "javascript:history.back();"! );! }! }! }! ! return $input;! } !
  • Guilherme Blanco PHP4Adults ! public function validateForm($filters=array(), $validators=array(), $options=null)! { ! $data = $_POST; ! ! $input = new Zend_Filter_Input($filters, $validators, $data, $options); ! $input->setDefaultEscapeFilter(new Zend_Filter_StringTrim()); ! ! if ( ! ($input->hasInvalid() || $input->hasMissing())) {! ! return $input;! ! }! ! foreach ($input->getMessages() as $field => $messageList) {! foreach ($messageList as $message) {! if (strpos($message, "empty")) {! throw new Tss_FormException(! "The field {$field} cannot be empty!", ! 3, ! "javascript:history.back();"! );! } else { ! throw new Tss_FormException(! "{$message}", ! 3, ! "javascript:history.back();"! );! }! }! }! ! return $input;! } ! 0 1 2 3
  • Guilherme Blanco PHP4Adults ! public function validateForm($filters=array(), $validators=array(), $options=null)! { ! $data = $_POST; ! ! $input = new Zend_Filter_Input($filters, $validators, $data, $options); ! $input->setDefaultEscapeFilter(new Zend_Filter_StringTrim()); ! ! if ( ! ($input->hasInvalid() || $input->hasMissing())) {! ! return $input;! ! }! ! foreach ($input->getMessages() as $field => $messageList) {! foreach ($messageList as $message) {! $errorMessage = (strpos($message, "empty") === false)! ? "The field {$field} cannot be empty!"! : $message;! ! throw new Tss_FormException(! $errorMessage, ! 3, ! "javascript:history.back();"! );! }! }! ! return $input;! } !
  • Guilherme Blanco PHP4Adults ! public function validateForm($filters=array(), $validators=array(), $options=null)! { ! $data = $_POST; ! ! $input = new Zend_Filter_Input($filters, $validators, $data, $options); ! $input->setDefaultEscapeFilter(new Zend_Filter_StringTrim()); ! ! if ( ! ($input->hasInvalid() || $input->hasMissing())) {! ! return $input;! ! }! ! foreach ($input->getMessages() as $field => $messageList) {! foreach ($messageList as $message) {! $errorMessage = (strpos($message, "empty") === false)! ? "The field {$field} cannot be empty!"! : $message;! ! throw new Tss_FormException(! $errorMessage, ! 3, ! "javascript:history.back();"! );! }! }! ! return $input;! } ! 0 1 2
  • Guilherme Blanco PHP4Adults ! public function validateForm($filters=array(), $validators=array(), $options=null)! { ! $data = $_POST; ! ! $input = new Zend_Filter_Input($filters, $validators, $data, $options); ! $input->setDefaultEscapeFilter(new Zend_Filter_StringTrim()); ! ! if ( ! ($input->hasInvalid() || $input->hasMissing())) {! ! return $input;! ! }! ! foreach ($input->getMessages() as $field => $messageList) {! $message = array_shift($messageList);! $errorMessage = (strpos($message, "empty") === false)! ? "The field {$field} cannot be empty!"! : $message;! ! throw new Tss_FormException(! $errorMessage, ! 3, ! "javascript:history.back();"! );! }! ! return $input;! } !
  • Guilherme Blanco PHP4Adults ! public function validateForm($filters=array(), $validators=array(), $options=null)! { ! $data = $_POST; ! ! $input = new Zend_Filter_Input($filters, $validators, $data, $options); ! $input->setDefaultEscapeFilter(new Zend_Filter_StringTrim()); ! ! if ( ! ($input->hasInvalid() || $input->hasMissing())) {! ! return $input;! ! }! ! foreach ($input->getMessages() as $field => $messageList) {! $message = array_shift($messageList);! $errorMessage = (strpos($message, "empty") === false)! ? "The field {$fie