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

Fantastic Bugs and How to Avoid Them

Fantastic Bugs and How to Avoid Them

Legacy code can be riddled with bugs — both ordinary and extraordinary. Quickly finding and conquering them is crucial for upgrades and for less painful maintenance. Join Anna Filina as she demonstrates fixes and avoidance strategies for some of the most prevalent bugs found in legacy code.

Anna Filina

June 24, 2021
Tweet

More Decks by Anna Filina

Other Decks in Programming

Transcript

  1. Anna Filina ‣ Coding since 1997. ‣ PHP since 2003.

    ‣ Legacy archaeologist. ‣ Test automation. ‣ Talks and workshops. ‣ YouTube videos.
  2. What is this about? ‣Real-world "silly" bugs. ‣How they came

    to be. ‣How you can avoid making mistakes. ‣Also, I need to rant about bad code.
  3. $paths = $this->getPaths(); $urls = array_map(function ($path) { return self::URL_ROOT

    . $path; }, $paths); array_map(): Expected parameter 2 to be an array, string given
  4. /** * @return array<int, string> */ private function getPaths(): array

    { return [ $this->path1, $this->path2, ]; } ERROR: MixedReturnTypeCoercion - src/TypeMismatch.php:65:16 - The type 'array{0: mixed, 1: mixed}' is more general than the declared return type 'array<int, string>' ...
  5. final class Path { private string $path; public function __construct(string

    $path) { Assert::that($path) ->notBlank(); $this->path = $path; } public function __toString(): string { return $this->path; } }
  6. /** * @return array<int, Path> */ private function getPaths(): array

    { return [ new Path('/products'), new Path('/cart'), ]; }
  7. class Product { public $name; } //... $this->findByName($this->product->name); TypeError :

    Argument 1 passed to MyClass::findByName() must be of the type string, null given
  8. class ProductEntity { public $name; public $price; } final class

    Product { private string $name; private int $price; //... }
  9. interface ApiAware { public function setApi(Api $api); } if ($class

    instanceof ApiAware) { $class->setApi($api); }
  10. final class MyClass implements ApiAware { private $api; public function

    setApi(Api $api): void { $this->api = $api; } public function sendApiRequest() { $product = new Product(); $this->api->sendRequest($product); } }
  11. final class MyClass { private Api $api; public function __construct(Api

    $api) { $this->api = $api; } public function sendApiRequest() { $product = new Product(); $this->api->sendRequest($product); } }