Giter Site home page Giter Site logo

phpstan-blade-rule's Introduction

phpstan-blade-rule

Build Status PHPStan level max Total Downloads Latest Version License

PHPStan rule for static analysis of Blade templates.


Installation

To use this extension, require it in Composer:

composer require --dev canvural/phpstan-blade-rule

If you also have phpstan/extension-installer installed, then you're all set!

Manual installation

If you don't want to use phpstan/extension-installer, include extension.neon in your project's PHPStan config:

includes:
    - vendor/canvural/phpstan-blade-rule/config/extension.neon
    - vendor/symplify/template-phpstan-compiler/config/services.neon
    - vendor/symplify/astral/config/services.neon

Configuration

You need to configure paths of views for the rule to scan using templatePaths config parameter key. Each path should be a relative path to your phpstan.neon config file.

For example for default Laravel installation, you can configure the paths like so:

parameters:
    templatePaths:
        - resources/views

โš ๏ธ The paths you add to templatePaths should be relative to the root of your app (for example where your vendor folder is or where your phpstan.neon is), and should not contain .. or .

Features

Custom error formatter

We provide custom PHPStan error formatter to better display the template errors. The custom error formatter extends the PHPStan's table error formatter and just adds additional information about template errors to the message.

An example:

To use this custom error formatter you need to run PHPStan with --error-format blade option. For example:

vendor/bin/phpstan analyse src -l8 --error-format blade

Known issues / TODOs

  • Custom directives are not supported. Can be supported by custom bootstrap file maybe.
  • Blade components are not analyzed. Support for it will come soon.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

People:

Resources:

License

The MIT License (MIT). Please see License File for more information.

phpstan-blade-rule's People

Contributors

canvural avatar owenvoke avatar thibauddauce avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

phpstan-blade-rule's Issues

This is so nice project

This projects allows one to exercise total control over blade templates.
@canvural What are your plans wit it?
I think it cannot be installed with L10, PHPStan 1.10

Internal error when used with PHPStan 1.8.x

Greetings.

Having PHPStan 1.8.4 installed beforehand, I gave this extension a try. It threw the following internal error:

     Internal error: Internal error: PHPStan\Analyser\FileAnalyser::analyseFile(): Argument #4 ($collectorRegistry) must be of type PHPStan\Collectors\Registry, null given, called in /var/www/project/vendor/canvural/phpstan-blade-rule/src/Rules/ViewRuleHelper.php on line 94 in file  
     /var/www/project/app/Http/Controllers/NegocioController.php                                                                                                                                                                                                                            
     Run PHPStan with -v option and post the stack trace to:                                                                                                                                                                                                                                                                   
     https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md                                                                                                                                                                                                                                                      
     Child process error (exit code 1):   

It turns out PHPStan 1.8.0 introduced a breaking change in its signature for method analyseFile , which now reads:

	public function analyseFile(
		string $file,
		array $analysedFiles,
		RuleRegistry $ruleRegistry,
		CollectorRegistry $collectorRegistry,
		?callable $outerNodeCallback,
	): FileAnalyserResult

whereas, up to version 1.7.15, the same method used to have the signature:

	public function analyseFile(
		string $file,
		array $analysedFiles,
		Registry $registry,
		?callable $outerNodeCallback,
	): FileAnalyserResult

For the time being I have resorted to downgrading and pinning PHPStan, but since I didn't see the issue reported elsewhere, I thought you might want to be given the heads up.

Thanks!

Unable to use `use` statements in a blade template that is included

I thought I'd just add an issue so that this doesn't get lost, however I'm currently looking into it a bit. ๐Ÿ‘๐Ÿป

See owenvoke#3 for a fixture that demonstrates this issue.

When using the @include() directive with a file that contains use statements, in standard Blade it compiles to separate files, whereas (AFAICT) this package compiles to a single file which is checked (correct me if I'm wrong). But this causes the use statements to fail as they are in the global scope. Resulting in a PHP syntax error.

The error that will be output is:

PhpParser\Error: Syntax error, unexpected T_USE on line 5

Looks like it's because the use statement is embedded inside a function block when it's compiled. ๐Ÿค” e.g.

<?php
(function ()  {
    use Illuminate\Support\Facades\URL;

    URL::current();
});

Syntax error, unexpected ')', expecting T_VARIABLE on line 2

Could be kinda related to #4

When I put your rules non commented phpstan outputs some PHP parse errors on files that have nothing to do with the extension

diff --git a/phpstan.neon b/phpstan.neon
index c8892207e..2ecac5269 100644
--- a/phpstan.neon
+++ b/phpstan.neon
@@ -1,6 +1,9 @@
 includes:
 	- ./vendor/nunomaduro/larastan/extension.neon
 	- ./phpstan-baseline.neon
+	- ./vendor/canvural/phpstan-blade-rule/config/extension.neon
+	- ./vendor/symplify/template-phpstan-compiler/config/services.neon
+	- ./vendor/symplify/astral/config/services.neon
 parameters:
 	level: max
 	paths:
@@ -9,3 +12,5 @@ parameters:
 		- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 		# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 		- xxxxxxxxxxxxxxxxxxxxxxxxxxx
+	templatePaths:
+		- resources/views

Using this container that runs PHP 8.1.3: https://hub.docker.com/r/lcde/laravel

Syntax error, unexpected T_ENDIF on line X

Hi, first of all thank you. I've been waiting to be able to run static analysis on my blade templates since a long time.

Unfortunately on my first try I get only a bunch of syntax errors such as:

 ------ ---------------------------------------------------------------
  Line   Domain/Schedulers/Http/Controllers/SchedulePlanController.php 
 ------ ---------------------------------------------------------------
  217    Syntax error, unexpected T_ENDIF on line 217
 ------ ---------------------------------------------------------------

or

 ------ --------------------------------------------------------------
  Line   Domain/Statistics/Http/Controllers/StatsController.php       
 ------ --------------------------------------------------------------
  6      Syntax error, unexpected T_VARIABLE, expecting ';' on line 6
 ------ --------------------------------------------------------------

What I can tell you is that, for example, SchedulePlanController doesn't even have 217 lines so, I guess, its referring to some issue in some of the blade templates rendered by the controller?

My composer.json is something like:

{
    "require": {
        "php": ">=8.0",
        "laravel/framework": "^8.0",
        "laravel/helpers": "^1.2",
        "laravel/legacy-factories": "^1.1",
        "laravel/ui": "^3.0",
    },
    "require-dev": {
        "canvural/phpstan-blade-rule": "^0.1.0",
        "nunomaduro/larastan": "^1.0",
    },
    "autoload": {
        "classmap": [
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/",
            "Domain\\": "app/Domain/",
            "Common\\": "app/Common/",
            "Database\\Seeders\\": "database/seeders/"
        },
        "files": [
            "app/Common/helpers.php"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    }
}

My phpstan.neon file:

includes:
    - ./vendor/nunomaduro/larastan/extension.neon
    - ./vendor/canvural/phpstan-blade-rule/config/extension.neon
    - ./vendor/symplify/template-phpstan-compiler/config/services.neon
    - ./vendor/symplify/astral/config/services.neon

parameters:

    paths:
        - app/Domain
        - app/Common

    templatePaths:
        - resources/views

    # The level 8 is the highest level
    level: 8

    #ignoreErrors:
    #    - '#Unsafe usage of new static#'

    excludePaths:
        - ./*/*/FileToBeExcluded.php

    checkMissingIterableValueType: false

I've also tried to comment larastan extension but nothing.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.