Static Analysis With Psalm
Tags: php, 100daysofcode, codenewbie, webdev
Published: 2021-04-23 08:20:24

---
title: Static Analysis With Psalm
tags: PHP, 100daysofcode, codenewbie, webdev
description: "Over the years there has been tremendous efforts towards development of automated tools and processes to prevent such catastrophic typos from finding their way into or making a home of our software code bases. Extensions of compiler technologies have been built to help us with automated reviews of our code."
cover_image: https://images.unsplash.com/photo-1618522284999-3430d5f5f2da?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1650&q=80
published: true
hidden: false
---

A <code>typo</code> is never that serious unless it's in a software code base. A tiny mistake such as an omission of a comma can [cause companies billions of dollars](https://www.inc.com/jeff-haden/how-1-missing-comma-just-cost-this-company-5-million-but-did-make-its-employees-5-million-richer.html), [destroy a NASA rocket](https://priceonomics.com/the-typo-that-destroyed-a-space-shuttle/) or even interrupt service for users of a SaaS product.

Over the years there has been tremendous efforts towards development of automated tools and processes to prevent such catastrophic typos from finding their way into or making a home of our software code bases. Extensions of compiler technologies have been built to help us with automated reviews. 

In this post, I write about Psalm - a PHP static analysis tool, and how you can take advantage of its powerful features in stopping bad code from creeping into your source codes.

### Psalm

Psalm is a VIMEO open-source project that helps you in catching bugs early and identifying problems in your code. It helps:

- prevent type-related runtime errors
- enforce standard coding patterns
- autofix bugs
- detect PHP security vulnerabilities using [taint analysis](https://dzone.com/articles/what-is-taint-analysis-and-why-should-i-care#:~:text=Taint%20analysis%20identifies%20every%20source,mean%20all%20the%20way%20through.)

For a quick start and installation guide, head over [here](https://psalm.dev/docs/running_psalm/installation/).

Psalm does not just flag a line of code as problematic, it includes links that you can follow to see explanation for the decision. It looks like this:

```bash
ERROR: InvalidArgument - \namespace\file.php:8:19 - 
Argument 1 of getAttribute expects string, 
int provided (see https://psalm.dev/004)
```

### Support For Laravel

Psalm v3 came with a new plugin framework that makes authoring integrations for third party packages much easier. That, with [Barry's IDE Helper](https://github.com/barryvdh/laravel-ide-helper) made the [Laravel Plugin](https://github.com/psalm/psalm-plugin-laravel) a reality according to Matt.

It's super easy to setup. You only need two commands after successfully installing and setting up Psalm:

```
composer require --dev psalm/plugin-laravel
./vendor/bin/psalm-plugin enable psalm/plugin-laravel
```

Under the hood, it runs the laravel IDE helper, get the stubs and feed them into Psalm. I, personally, would love it that the package exists independent of the IDE helper because a scenario in the future where issues would pile up in both repos because of some dependency mismatch or recent upgrade that stops a certain *"something"* from working entirely is imminent.

But, so far so good. I'm really happy with the developments and excited for what is in store for the future!

This post first appeared [here](https://faruknasir.com/2021/4/static-analysis-with-psalm)