DEV Community

Cover image for Peck is a utility for your PHP projects
Denis
Denis

Posted on

Peck is a utility for your PHP projects

Peck is a powerful CLI tool designed to identify wording or writing errors in your codebase: file names, class names, method names, property names, documents, and more.

It often happens that we can incorrectly name a class name, variable name, or file name. It is not always possible to notice this due to various factors, and then there may be typos in the project, which can lead to code complexity and confusion in reading.

Therefore, such a tool will be able to solve this problem, as it can be easily implemented into your pipline, which will allow you to transfer the spell-checking process to a machine, making your life easier.

The installation process

To install the package, you will need php 8.2 and higher, as well as GNU Aspell

GNU Aspell is a free spell—checking program designed to replace Ispell and serve as a standard spell-checking tool in the GNU operating system

Adding this utility to our Dockerfile with php:

RUN apt-get update \  
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \  
    git \  
    unzip \  
    librabbitmq-dev \  
    libpq-dev \  
    aspell  \  
    aspell-en \  
    supervisor
Enter fullscreen mode Exit fullscreen mode

And already aspell and aspell-en have been added to the necessary packages

aspell-en - contains spelling checker files for English

Install the package via composer:

composer require peckphp/peck --dev
Enter fullscreen mode Exit fullscreen mode

Initialize the configuration for the package:

./vendor/bin/peck --init
Enter fullscreen mode Exit fullscreen mode

After Initialization, the file peck.json:

{
    "preset": "laravel",
    "ignore": {
        "words": [
            "config",
            "namespace"
        ],
        "paths": [
            "app/MyFolder",
            "app/MyFile.php"
        ]
    }
}
Enter fullscreen mode Exit fullscreen mode

The example provided by the developers

Description of the configuration keys:

words - specify the words that will not be used for spell checking

paths - specify the paths where folders or files will be ignored

in my project, I had to add more words to ignore:

{  
  "preset": "laravel",  
  "ignore": {  
    "words": [  
      "php",  
      "dto",  
      "sha",  
      "centrifugo",  
      "js",  
      "postcss",  
      "phpcs",  
      "makefile",  
      "cors",  
      "filesystems",  
      "websocket",  
      "favicon"  
    ],  
    "paths": [  
      "/public/vendor",  
      "/public/fonts",  
      "/public/js",  
      "/lang/vendor"  
    ]  
  }  
}
Enter fullscreen mode Exit fullscreen mode

We can also move our config to another location and explicitly specify it using the command with the --config flag, where its argument will be the path to the file.

Example:

./vendor/bin/peck --config relative/path/to/peck.json
Enter fullscreen mode Exit fullscreen mode

Now we can run the verification package:

./vendor/bin/peck
Enter fullscreen mode Exit fullscreen mode

I found an error in the file name in the project.:

Misspelling  ./documentation/code_standart.md: 'standart'  

Did you mean: standard, standout, standards, stander
Enter fullscreen mode Exit fullscreen mode

Also, for convenience, add this command to the composer scripts:

...
{
    "scripts": {
        "word-check": "@php vendor/bin/peck",
    }
}
Enter fullscreen mode Exit fullscreen mode

And you can already call this check with the command - composer word-check

Bottom line

Although this package does not have a stable release, you can still try to install it in your projects and check for the correctness of expressions in your code base.

It's up to you to use it or not, the most important thing is to improve the quality of the codebase using the tools that you consider necessary.

I also implemented this tool in my project - laravel + frankenphp template


GitHub - I will be glad to receive your subscription to me in github

Thank you for reading this article.

Top comments (0)