Skip to content

PHPStan rules for checking validity of links in Nette applications

License

Notifications You must be signed in to change notification settings

MetisFW/phpstan-nette-links

Repository files navigation

Nette Framework link validation for PHPStan

Build License

Check links in your Nette Framework applications using PHPStan.

Preview

Features

  • Validate parameters passed to link(), lazyLink(), redirect(), redirectPermanent(), forward(), isLinkCurrent() and canonicalize() methods
  • Works for presenters, components and 'LinkGenerator' service
  • Checks if passed destination is valid and points to existing presenter, action or signal
  • Checks if passed link parameters are valid and match relevant action*(), render*() or handle*() method signature
  • Checks also links to sub-components of known types (createComponent*() method must exists)

Installation

To use this extension, require it in Composer:

composer require --dev metisfw/phpstan-nette-links

If you also install phpstan/extension-installer 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/metisfw/phpstan-nette-links/extension.neon

To perform framework-specific checks, include also this file:

    - vendor/metisfw/phpstan-nette-links/rules.neon

Configuration

Either applicationMapping or containerLoader (for automatically loading mappings from PresenterFactory service in your app) must be set for link checking to work.

If you use non-standard PresenterFactory this feature might not work because logic for mapping presenter name (e.g. MyModule:Homepage) to presenter class (e.g. \App\Presenters\MyModule\HomepagePresenter) and vice versa would work differently.

If you use containerLoader you might solve this by implementing method unformatPresenterClass in your custom PresenterFactory class. This method should return presenter name for given presenter class.

Or you can create custom implementation overriding PHPStanNetteLinks\Nette\PresenterResolver service and replace it in your PHPStan config:

services:
    nettePresenterResolver:
        class: MyCustom\PresenterResolver

containerLoader

Container loader can be used to create instance of Nette application DI container.

Example:

parameters:
    netteLinks:
        containerLoader: './containerLoader.php'

Example containerLoader.php:

<?php

return App\Bootstrap::boot()->createContainer();

applicationMapping

Application mapping is used to map presenter identfiers to classes in link checking.

Example:

parameters:
    netteLinks:
        applicationMapping:
            *: App\Presenters\*\*Presenter

About

PHPStan rules for checking validity of links in Nette applications

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •