What Sanity Checks Do You Use in Your Projects?

Hey fellow developers!

I wanted to start a discussion about the different types of sanity checks we use in our projects to ensure code quality, maintainability, and overall reliability before pushing changes live.

Sanity checks are critical in catching small but impactful issues early, whether it’s debugging leftovers, unused variables, or even forgotten dd() statements in Laravel (guilty :sweat_smile:). Sometimes, automated tools in our CI/CD pipelines help, and sometimes it’s manual—but the goal is always to ensure our code is polished and ready for production.

Here are some sanity checks I’ve used or seen:

1. Debugging Statements Check

  • In Laravel, I’ve added a grep check in my GitLab CI pipeline to catch dd() or dump() functions before pushing to production. It fails the pipeline if any of those are found.

2. PHP Code Sniffer & PHPStan

  • Using these tools to automatically enforce coding standards and prevent errors or unnecessary complexity in my PHP projects.

3. API Response Validation

  • Adding sanity checks for expected JSON structure and response types when consuming APIs during testing phases.

4. Hardcoded URLs or Credentials

  • Regularly scanning for hardcoded credentials, API keys, or URLs in the codebase. We’ve all seen the consequences of accidentally shipping a hardcoded password :grimacing:.

5. Unused Variables/Imports

  • Sanity checks for unused variables, classes, or imports using static analysis tools to avoid unnecessary bloat in the code.

6. Database Queries

  • Ensuring that SQL queries are properly optimized, and there aren’t any unintentional N+1 queries, especially in ORM-heavy projects.

I’d love to hear what sanity checks you all use! :rocket:

Feel free to share any custom checks you’ve written, tools you rely on, or even the simple habits you’ve picked up over time that help you ship better code. Let’s build a comprehensive list that can benefit everyone!

Looking forward to your contributions! :blush:

For me, I mainly use Laravel for backend and recently someone left a dd() in their merge request that resulted in an infinite loop in the CICD pipeline. To check for the use of the dd() function in a Laravel project within a GitLab CI/CD pipeline, you can create a custom job in your .gitlab-ci.yml file that scans the codebase for any instances of dd(). This can be done using tools like grep or PHPStan to enforce coding standards and ensure that debugging functions like dd() are not present in production code.

Here’s an example of how you can add a job to your pipeline:

.gitlab-ci.yml

stages:
  - lint

lint_dd_check:
  stage: lint
  script:
    - if grep -R 'dd(' app/; then echo "dd() found in the codebase!"; exit 1; else echo "No dd() found."; fi
  allow_failure: false

Breakdown:

  1. Stages: Define a lint stage in the pipeline.
  2. lint_dd_check: A job to search for any instances of the dd() function.
  3. grep -R ‘dd(’ app/: This command recursively searches the app/ directory for the dd() function.
  4. exit 1: If dd() is found, the script exits with an error to fail the pipeline.
  5. allow_failure: false: Ensures that the pipeline fails if dd() is detected.

You can expand this job to cover more directories or specific files based on your project structure.