Convert Figma logo to code with AI

larastan logolarastan

⚗️ Adds code analysis to Laravel improving developer productivity and code quality.

5,410
402
5,410
75

Top Related Projects

🔰 Instant PHP quality checks from your console

5,537

A static analysis tool for finding errors in PHP applications

12,802

PHP Static Analysis Tool - discover bugs in your code without running it!

5,528

Phan is a static analyzer for PHP. Phan prefers to avoid false-positives and attempts to prove incorrectness rather than correctness.

Quick Overview

Larastan is a static analysis tool for Laravel applications, built on top of PHPStan. It provides additional Laravel-specific rules and checks, helping developers catch potential errors and improve code quality in their Laravel projects without running the code.

Pros

  • Enhances code quality and reduces bugs in Laravel applications
  • Integrates seamlessly with Laravel's ecosystem and conventions
  • Customizable and extendable to fit specific project needs
  • Improves developer productivity by catching errors early in the development process

Cons

  • May have a learning curve for developers new to static analysis tools
  • Can produce false positives in some cases, requiring configuration tweaks
  • Might slow down the development process initially as developers adjust to stricter coding standards
  • Limited to Laravel-specific features and may not cover all edge cases

Code Examples

  1. Basic configuration in phpstan.neon:
includes:
    - ./vendor/nunomaduro/larastan/extension.neon

parameters:
    paths:
        - app
    level: 5

This configuration includes Larastan's extension and sets the analysis level to 5.

  1. Custom rule implementation:
<?php

namespace App\PHPStan;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;

class CustomRule implements Rule
{
    public function getNodeType(): string
    {
        return Node\Expr\MethodCall::class;
    }

    public function processNode(Node $node, Scope $scope): array
    {
        // Custom rule logic here
        return [];
    }
}

This example shows how to create a custom rule for Larastan.

  1. Ignoring errors in specific files:
/**
 * @phpstan-ignore-next-line
 */
$result = $this->dangerousMethod();

This comment tells Larastan to ignore any errors on the following line.

Getting Started

  1. Install Larastan via Composer:

    composer require --dev nunomaduro/larastan
    
  2. Create a phpstan.neon file in your project root:

    includes:
        - ./vendor/nunomaduro/larastan/extension.neon
    
    parameters:
        paths:
            - app
        level: 5
    
  3. Run Larastan:

    ./vendor/bin/phpstan analyse
    

Competitor Comparisons

🔰 Instant PHP quality checks from your console

Pros of PHPInsights

  • Provides a broader analysis of code quality, including architecture, style, and complexity metrics
  • Offers a user-friendly CLI interface with colorful output and progress bars
  • Includes customizable rulesets and the ability to create custom insights

Cons of PHPInsights

  • May produce more false positives compared to Larastan's focused static analysis
  • Requires more configuration to tailor the analysis to specific project needs
  • Can be slower to run on large codebases due to its comprehensive analysis

Code Comparison

Larastan:

use Illuminate\Support\Facades\DB;

function getUserNames(): array
{
    return DB::table('users')->pluck('name')->toArray();
}

PHPInsights:

use Illuminate\Support\Facades\DB;

function getUserNames(): array
{
    $users = DB::table('users')->get();
    return $users->pluck('name')->toArray();
}

While both tools can analyze these code snippets, PHPInsights might provide additional insights on code style and complexity, whereas Larastan would focus more on type-related issues and potential runtime errors.

5,537

A static analysis tool for finding errors in PHP applications

Pros of Psalm

  • More extensive type checking and static analysis capabilities
  • Supports a wider range of PHP projects beyond Laravel
  • Offers more customization options and configuration flexibility

Cons of Psalm

  • Steeper learning curve for new users
  • May require more setup and configuration for Laravel projects
  • Can be more resource-intensive for large codebases

Code Comparison

Larastan configuration:

<?php

return [
    'paths' => [
        'app',
        'tests',
    ],
    'level' => 5,
];

Psalm configuration:

<?xml version="1.0"?>
<psalm
    errorLevel="3"
    resolveFromConfigFile="true"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="https://getpsalm.org/schema/config"
    xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
    <projectFiles>
        <directory name="src" />
        <ignoreFiles>
            <directory name="vendor" />
        </ignoreFiles>
    </projectFiles>
</psalm>

Both tools aim to improve PHP code quality through static analysis, but Psalm offers more advanced features and broader project support, while Larastan is tailored specifically for Laravel applications with easier setup and integration.

12,802

PHP Static Analysis Tool - discover bugs in your code without running it!

Pros of PHPStan

  • More general-purpose, can be used with any PHP project
  • Larger community and more frequent updates
  • Extensive rule set and customization options

Cons of PHPStan

  • Requires more configuration for Laravel-specific features
  • May produce false positives for Laravel-specific code patterns
  • Steeper learning curve for Laravel developers

Code Comparison

PHPStan configuration:

<?php
// phpstan.neon
parameters:
    level: 5
    paths:
        - src
    excludes_analyse:
        - vendor

Larastan configuration:

<?php
// phpstan.neon.dist
includes:
    - ./vendor/nunomaduro/larastan/extension.neon

parameters:
    level: 5
    paths:
        - app

Summary

PHPStan is a powerful static analysis tool for PHP projects, offering extensive customization and a large community. However, for Laravel-specific projects, Larastan provides a more tailored experience with built-in support for Laravel conventions and patterns. While PHPStan requires more setup for Laravel projects, it offers greater flexibility for non-Laravel PHP applications. Larastan, being built on top of PHPStan, inherits its core functionality while adding Laravel-specific features, making it a more convenient choice for Laravel developers.

5,528

Phan is a static analyzer for PHP. Phan prefers to avoid false-positives and attempts to prove incorrectness rather than correctness.

Pros of Phan

  • More extensive analysis capabilities, covering a wider range of PHP issues
  • Language-agnostic, can be used with any PHP project
  • Highly configurable with numerous options for customization

Cons of Phan

  • Steeper learning curve due to its complexity and extensive configuration options
  • May produce more false positives, requiring additional effort to fine-tune
  • Less integrated with Laravel-specific features and conventions

Code Comparison

Phan configuration:

return [
    'target_php_version' => '7.4',
    'directory_list' => ['src/', 'vendor/'],
    'exclude_analysis_directory_list' => ['vendor/'],
    'plugins' => ['UnreachableCodePlugin'],
];

Larastan configuration:

return [
    'paths' => [
        'app',
        'config',
        'database',
        'routes',
    ],
    'level' => 5,
];

Summary

Phan is a more general-purpose PHP static analyzer with extensive capabilities, while Larastan is tailored specifically for Laravel projects. Phan offers greater flexibility and customization but requires more setup and fine-tuning. Larastan provides a more streamlined experience for Laravel developers with out-of-the-box support for framework-specific features and conventions.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Larastan Logo

Larastan Example

Build Status Total Downloads Latest Version License


⚗️ About Larastan

If you are using a Laravel version older than 9.x, please refer to Larastan v1.x with PHPStan 1.8.x.

Larastan was created by Can Vural and Nuno Maduro, got artwork designed by @Caneco, is maintained by Can Vural, Nuno Maduro, and Viktor Szépe, and is a PHPStan wrapper for Laravel. Larastan focuses on finding errors in your code. It catches whole classes of bugs even before you write tests for the code.

  • Adds static typing to Laravel to improve developer productivity and code quality
  • Supports most of Laravel's beautiful magic
  • Discovers bugs in your code

While by definition, "static analysis" doesn't load any of your application's code. Larastan boots your application's container, so it can resolve types that are only possible to compute at runtime. That's why we use the term "code analysis" instead of "static analysis".

✨ Getting Started In 3 Steps

Requires:

1: First, you may use Composer to install Larastan as a development dependency into your Laravel project:

composer require --dev "larastan/larastan:^2.0"

Using Larastan for analysing Laravel packages? You may need to install orchestra/testbench.

2: Then, create a phpstan.neon or phpstan.neon.dist file in the root of your application. It might look like this:

includes:
    - vendor/larastan/larastan/extension.neon

parameters:

    paths:
        - app/

    # Level 9 is the highest level
    level: 5

#    ignoreErrors:
#        - '#PHPDoc tag @var#'
#
#    excludePaths:
#        - ./*/*/FileToBeExcluded.php
#
#    checkMissingIterableValueType: false

For all available options, please take a look at the PHPStan documentation: https://phpstan.org/config-reference

3: Finally, you may start analyzing your code using the phpstan console command:

./vendor/bin/phpstan analyse

If you are getting the error Allowed memory size exhausted, then you can use the --memory-limit option fix the problem:

./vendor/bin/phpstan analyse --memory-limit=2G

Ignoring errors

Ignoring a specific error can be done either with a php comment or in the configuration file:

// @phpstan-ignore-next-line
$test->badMethod();

$test->badMethod(); // @phpstan-ignore-line

When ignoring errors in PHPStan's configuration file, they are ignored by writing a regex based on error messages:

parameters:
    ignoreErrors:
        - '#Call to an undefined method .*badMethod\(\)#'

Baseline file

In older codebases it might be hard to spend the time fixing all the code to pass a high PHPStan Level.

To get around this a baseline file can be generated. The baseline file will create a configuration file with all of the current errors, so new code can be written following a higher standard than the old code. (PHPStan Docs)

./vendor/bin/phpstan analyse --generate-baseline

Rules

A list of configurable rules specific to Laravel can be found here.

Features

A list of Larastan features can be found here.

Custom PHPDoc types

A list of PHPDoc types specific to Larastan can be found here.

Custom PHPStan config parameters

A list of custom config parameters that you can use in your PHPStan config file can be found here.

Errors To Ignore

Some parts of Laravel are currently too magical for Larastan/PHPStan to understand. We listed common errors to ignore, add them as needed

👊🏻 Contributing

Thank you for considering contributing to Larastan. All the contribution guidelines are mentioned here.

📖 License

Larastan is an open-sourced software licensed under the MIT license.