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

Static Code Analysis: Judging a Forest by Its T...

Static Code Analysis: Judging a Forest by Its Trees

Static code analysis can help you automate the boring parts of your code reviews. This talk is an overview of several tools and tips on how to use them in new and existing projects, featuring the following tools that can help you: Stop nit-picking your colleagues’ code style (Easy Coding Standard), keep up with best practices (Psalm, PHPStan, PHP Insights) and generate graphs visualising your dependencies (Deptrac, PhpMetrics).

Christian Rades

March 12, 2020
Tweet

More Decks by Christian Rades

Other Decks in Programming

Transcript

  1. JUDGING A FOREST BY JUDGING A FOREST BY IT’S TREES

    IT’S TREES Static PHP code analysis
  2. YOUR APP IS A FORREST YOUR APP IS A FORREST

    IT CONTAINS LOTS OF TREES IT CONTAINS LOTS OF TREES
  3. 2 3

  4. CUSTOM RULES CUSTOM RULES services: - class: ...\Rules\Decoratable\DecoratableImplementsInterfaceRule tags: -

    phpstan.rules.rule - class: ...\Rules\Decoratable\DecoratableDoesNotAddPublicMethodRule tags: - phpstan.rules.rule - class: ...\Rules\Decoratable\DecoratableDoesNotCallOwnPublicMethodRule tags: - phpstan.rules.rule - class: ...\Rules\Decoratable\DecoratableNotDirectlyDependetRule tags: - phpstan.rules.rule
  5. TYPED ARRAYS TYPED ARRAYS /** @var array<Foo> */ $v =

    [new Foo()]; /** @var array<int, Foo> */ $v = [new Foo()];
  6. OBJECT LIKE ARRAYS OBJECT LIKE ARRAYS /** @var array{value: Foo,

    name: string} */ $v = ['value' => new Foo(), 'name' => 'fooInstance'];
  7. ERROR: InvalidScalarArgument - src/main.php:11:13 - Argument 2 of usort expects

    callable(mixed, mixed):int, Closure(mixed, mixed):bool provided usort($arr, function ($a, $b) { return $a['type'] > $b['type']; });
  8. Note: If two members compare as equal, their relative order

    in the sorted array is undefined. www.php.net/manual/en/function.usort.php
  9. depfile.yml layers: - name: Cdn collectors: - type: className regex:

    Cdn - name: Common collectors: - type: className regex: Common - name: SalesData collectors: - type: className regex: SalesData - name: SDK collectors: - type: className regex: SDK
  10. depfile.yml ruleset: Cdn: - SDK - Common Common: - SDK

    - Common SalesData: - SDK - Common SDK: - SDK - Common SocialNetwork: - SDK - Common Updater: - SDK - Common
  11. PHPMETRICS PHPMETRICS Lines of code Cyclomatic complexity Distribution of LOC

    per class Average bugs by class Afferent coupling Efferent coupling Class relationships
  12. tools/vendor-bin ├── ecs │ ├── composer.json │ ├── composer.lock │

    └── vendor └── phpstan ├── composer.json ├── composer.lock └── vendor
  13. MAKEFILE MAKEFILE ecs-dry: | install-tools vendor ## runs easy coding

    standard in dry mode $(TOOLS_BIN)/ecs check . ecs-fix: | install-tools vendor ## runs easy coding standard and fixes issues $(TOOLS_BIN)/ecs check . --fix
  14. static-analysis: | install-tools vendor ## runs psalm and phpstan $(TOOLS_BIN)/psalm

    --output-format=compact $(TOOLS_BIN)/phpstan analyze --configuration phpstan.neon src $(TOOLS_BIN)/phpinsights --no-interaction --min-quality=100 --min-complexi