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.

A5f6a4724de2c2986c8cbd4f46d52a7c?s=128

Gabriel Caruso

October 28, 2018
Tweet

Transcript

  1. Strict programming in PHP by Gabriel Caruso phpCE 2018

  2. Backend Developer at Leroy Merlin PHP Internals and Externals Evangelist

    PHPSP Member @carusogabriel
  3. 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
  4. What does strict programming mean?

  5. 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
  6. Benefits of using a strict programming language

  7. - Built-in scalar types - More strongly-typed code - Closed

    API code - Less Runtime exceptions - Know the flow of your code
  8. strict programming in PHP

  9. Equal operator <?php declare(strict_types=1);php var_dump( 'string' == 0, // bool(true)

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

  11. Equal operator <?php declare(strict_types=1);php var_dump( 'string' == 0, // bool(true)

    );
  12. Equal operator <?php declare(strict_types=1);php var_dump( 'string' == 0, // bool(true)

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

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

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

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

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

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

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

    identical );
  20. <?php declare(strict_types=1); de var_dump( $a === $b, // They are

    identical $a ==== $b, // They are in the same memory slot );
  21. <?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 );
  22. <?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 );
  23. <?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 );
  24. <?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 );
  25. Empty() Function <?php declare(strict_types=1); var_dump( empty(0), // bool(true) empty('0'), //

    bool(true) empty(false), // bool(true) );
  26. Use PHP instead! <?php declare(strict_types=1); var_dump( 0 !== 0, //

    bool(false) '0' !== '', // bool(false) false !== false, // bool(true) );
  27. <?php declare(strict_types=1); var_dump( is_null([]), );

  28. <?php declare(strict_types=1); var_dump( is_null([]), // bool(false) );

  29. is_null() only checks for the null type <?php declare(strict_types=1); var_dump(

    is_null([]), // bool(false) );
  30. is_null() only checks for the null type <?php declare(strict_types=1); var_dump(

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

    declare(strict_types=1); var_dump( [] !== null, // bool(true) );
  32. <?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);
  33. 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"
  34. <?php declare(strict_types=1); $foo = -1; if ($foo) { echo 'Hi';

    } else { echo 'Hello'; }
  35. if conditions aren’t that trustable <?php declare(strict_types=1); $foo = -1;

    if ($foo) { echo 'Hi'; // Seriously PHP? Like, really? } else { echo 'Hello'; }
  36. Only pass booleans to if conditions

  37. What about the php 7 scalar types?

  38. Weak scalar types do casting

  39. Use Strict scalar types!

  40. *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
  41. How to ensure that my project is strict safe?

  42. Static analysis

  43. Coding standards

  44. strict changes for PHP’s core?

  45. Typed Properties in PHP 7.4

  46. 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
  47. Thank you ♡ Questions? @carusogabriel