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

Strict programming in PHP

Strict programming in PHP

This talk will address several points in our codes that we can improve it to make it more stringent, avoiding false positives, points that have not been tested and have actually written code that complies with our business rules, avoiding unnecessary bugs. We will work with PHP 7 types, the identical operator, weak comparison functions and counters, and more.

Gabriel Caruso

October 28, 2018
Tweet

More Decks by Gabriel Caruso

Other Decks in Programming

Transcript

  1. Agenda - What does strict programming means - Benefits of

    using a strict programming language - Coding strict with PHP - How to ensure that my project is strict safe - Strict changes for PHP’s core
  2. Differences Strict programming A way, a paradigm, in how we

    can treat our coding Strict programming language A language that is build on top of strict programming paradigm
  3. - Built-in scalar types - More strongly-typed code - Closed

    API code - Less Runtime exceptions - Know the flow of your code
  4. Equal operator <?php declare(strict_types=1);php var_dump( 'string' == 0, // bool(true)

    [] == null, // bool(true) '' == false, // bool(true) 01 == 0001, // bool(true) );
  5. Equal operator <?php declare(strict_types=1);php var_dump( 'string' == 0, // bool(true)

    [] == null, // bool(true) '' == false, // bool(true) );
  6. Equal operator <?php declare(strict_types=1);php var_dump( 'string' == 0, // bool(true)

    [] == null, // bool(true) '' == false, // bool(true) 01 == 0001, );
  7. Equal operator <?php declare(strict_types=1);php var_dump( 'string' == 0, // bool(true)

    [] == null, // bool(true) '' == false, // bool(true) 01 == 0001, // bool(true) );
  8. Identical operator <?php declare(strict_types=1); de var_dump( 'string' === 0, //

    bool(false) [] === null, // bool(false) '' === false, // bool(false) '01' === '0001', // bool(false) );
  9. <?php declare(strict_types=1); de var_dump( $a === $b, // They are

    identical $a ==== $b, // They are in the same memory slot );
  10. <?php declare(strict_types=1); de var_dump( $a === $b, // They are

    identical $a ==== $b, // They are in the same memory slot $a ===== $b, // They live close to each other );
  11. <?php declare(strict_types=1); de var_dump( $a === $b, // They are

    identical $a ==== $b, // They are in the same memory slot $a ===== $b, // They live close to each other $a ====== $b, // They are brothers );
  12. <?php declare(strict_types=1); de var_dump( $a === $b, // They are

    identical $a ==== $b, // They are in the same memory slot $a ===== $b, // They live close to each other $a ====== $b, // They are brothers $a ======= $b, // They are twin brothers );
  13. <?php declare(strict_types=1); de var_dump( $a === $b, // They are

    identical $a ==== $b, // They are in the same memory slot $a ===== $b, // They live close to each other $a ====== $b, // They are brothers $a ======= $b, // They are twin brothers $a ======== $b, // They are the same );
  14. Use PHP instead! <?php declare(strict_types=1); var_dump( 0 !== 0, //

    bool(false) '0' !== '', // bool(false) false !== false, // bool(true) );
  15. is_null() only checks for the null type <?php declare(strict_types=1); var_dump(

    is_null([]), // bool(false) empty([null, null]), // bool(false) );
  16. But use the identical operator when you aren’t sure <?php

    declare(strict_types=1); var_dump( [] !== null, // bool(true) );
  17. <?php declare(strict_types=1); $foo = 1; switch ($foo) { case true;

    $bar = '$foo is equal to true'; break; case 1: $bar = '$foo is equal to 1'; break; } var_dump($bar);
  18. switch conditions are loosely compare <?php declare(strict_types=1); $foo = 1;

    switch ($foo) { case true; $bar = '$foo is equal to true'; break; case 1: $bar = '$foo is equal to 1'; break; } var_dump($bar); // string(21) "$foo is equal to true"
  19. if conditions aren’t that trustable <?php declare(strict_types=1); $foo = -1;

    if ($foo) { echo 'Hi'; // Seriously PHP? Like, really? } else { echo 'Hello'; }
  20. *Only non-NaN floats between PHP_INT_MIN and PHP_INT_MAX are accepted †Non-numeric

    strings are not accepted ‡Only if the object has a __toString() method inductions of scalar types
  21. References you can study beyond this talk - https://wiki.php.net/rfc/scalar_type_hints_v5#behaviour_o f_weak_type_checks

    - https://php.net/types-comparisons - https://www.phparch.com/2018/04/testing-strategy-with-the -help-of-static-analysis/ - https://github.com/phpstan/phpstan-strict-rules - https://github.com/slevomat/coding-standard