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

Defensive Programming Redux

Defensive Programming Redux

Defensive programming may sound like something your granddad did after the war, but it's key to reducing the number of bugs and increasing maintainability. We're going to look at what defensive programming is and some steps to doing it in PHP.

Christopher Pitt

August 26, 2014
Tweet

More Decks by Christopher Pitt

Other Decks in Programming

Transcript

  1. if (filter_var($_GET["name"], FILTER_VALIDATE_EMAIL)) { // email is valid } if

    (filter_var($_GET["age"], FILTER_VALIDATE_INT)) { // age is valid } if (filter_var($_GET["url"], FILTER_VALIDATE_URL)) { // url is valid }
  2. if ($email = filter_var($_GET["name"], FILTER_SANITIZE_EMAIL)) { // $email is valid

    } if ($age = filter_var($_GET["age"], FILTER_SANITIZE_NUMBER_INT)) { // $age is valid } if ($url = filter_var($_GET["url"], FILTER_SANITIZE_URL)) { // $url is valid }
  3. <div class="comments"> <?php foreach ($comments as $comment): ?> <div class="comment">

    <?php echo htmlentities($comment); ?> </div> <?php endforeach; ?> </div>
  4. $pdo = new PDO("sqlite:users.db"); $statement = $pdo->prepare("SELECT name FROM users

    where id = :id"); $statement->bindParam(":id", $_GET["id"], PDO::PARAM_INT); $statement->execute();
  5. interface Transport { public function send($to, $from, $subject, $message); }

    class MailTransport implements Transport { public function send($to, $from, $headers, $body) { // ... } }
  6. class Email { protected $transport; public function __construct(Transport $transport) {

    $this->transport = $transport; } public function send() { // $this->transport->send(); } }
  7. class Transaction { public function charge() { // ... }

    public function notify() { // ... } }
  8. class Transaction { protected $charger; protected $notifier; public function __construct(Charger

    $charger, Notifier $notifier) { $this->charger = $charger; $this->notifier = $notifier; } // ... }
  9. function charge(Card $card) { // ... } function notify(array $errors)

    { // ... } function process(callable $success, callable $error) { // ... }
  10. function addCustomer($name, $age) { assert(is_string($name), "Name is not a string.");

    assert(is_int($age), "Age is not an integer."); // ... }
  11. class Transaction { public $charger; public $notifier; } $transport =

    new Transport($charger, $notifier); $transport->charger = "moo"; // ...a great big error!
  12. $user = $repo->find($id); if (!$user) { return null; } $address

    = $user->getAddress(); if (!$address) { return null; } return $address->asText();
  13. class StringObject { protected $data; public function __construct($data) { $this->data

    = $data; } public function value() { return $this->data; } }
  14. function log(StringObject $string) { // look ma, no assert()! echo

    $string->value(); } $string = new StringObject("hello world"); log($string);
  15. /** * @param string $pattern * @param string $string *

    @return string[] */ function split($pattern, $string) { return preg_split($regex, $string, -1, PREG_SPLIT_NO_EMPTY); }