mirror of
https://github.com/danog/psalm-plugin-laravel.git
synced 2024-11-26 20:34:48 +01:00
Merge pull request #50 from mr-feek/url-helper
feature: add return type info for url helper
This commit is contained in:
commit
31b48d7f58
@ -6,6 +6,7 @@ use Illuminate\View\Engines\PhpEngine;
|
||||
use Illuminate\View\Factory;
|
||||
use Illuminate\View\FileViewFinder;
|
||||
use Orchestra\Testbench\Concerns\CreatesApplication;
|
||||
use Psalm\LaravelPlugin\ReturnTypeProvider\UrlReturnTypeProvider;
|
||||
use Psalm\Plugin\PluginEntryPointInterface;
|
||||
use Psalm\Plugin\RegistrationInterface;
|
||||
use SimpleXMLElement;
|
||||
@ -54,6 +55,8 @@ class Plugin implements PluginEntryPointInterface
|
||||
$registration->registerHooksFromClass(AppInterfaceProvider::class);
|
||||
require_once 'PropertyProvider/ModelPropertyProvider.php';
|
||||
$registration->registerHooksFromClass(PropertyProvider\ModelPropertyProvider::class);
|
||||
require_once 'ReturnTypeProvider/UrlReturnTypeProvider.php';
|
||||
$registration->registerHooksFromClass(UrlReturnTypeProvider::class);
|
||||
|
||||
$this->addOurStubs($registration);
|
||||
}
|
||||
|
31
src/ReturnTypeProvider/UrlReturnTypeProvider.php
Normal file
31
src/ReturnTypeProvider/UrlReturnTypeProvider.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Psalm\LaravelPlugin\ReturnTypeProvider;
|
||||
|
||||
use Illuminate\Contracts\Routing\UrlGenerator;
|
||||
use Psalm\CodeLocation;
|
||||
use Psalm\Context;
|
||||
use Psalm\Plugin\Hook\FunctionReturnTypeProviderInterface;
|
||||
use Psalm\StatementsSource;
|
||||
use Psalm\Type;
|
||||
use Psalm\Type\Atomic\TNamedObject;
|
||||
use Psalm\Type\Union;
|
||||
|
||||
final class UrlReturnTypeProvider implements FunctionReturnTypeProviderInterface
|
||||
{
|
||||
public static function getFunctionIds(): array
|
||||
{
|
||||
return ['url'];
|
||||
}
|
||||
|
||||
public static function getFunctionReturnType(StatementsSource $statements_source, string $function_id, array $call_args, Context $context, CodeLocation $code_location)
|
||||
{
|
||||
if (!$call_args) {
|
||||
return new Union([
|
||||
new TNamedObject(UrlGenerator::class),
|
||||
]);
|
||||
}
|
||||
|
||||
return Type::getString();
|
||||
}
|
||||
}
|
43
tests/acceptance/UrlReturnType.feature
Normal file
43
tests/acceptance/UrlReturnType.feature
Normal file
@ -0,0 +1,43 @@
|
||||
Feature: url
|
||||
The global url helper will return the correct type depending on args
|
||||
|
||||
Background:
|
||||
Given I have the following config
|
||||
"""
|
||||
<?xml version="1.0"?>
|
||||
<psalm totallyTyped="true">
|
||||
<projectFiles>
|
||||
<directory name="."/>
|
||||
<ignoreFiles> <directory name="../../vendor"/> </ignoreFiles>
|
||||
</projectFiles>
|
||||
<plugins>
|
||||
<pluginClass class="Psalm\LaravelPlugin\Plugin"/>
|
||||
</plugins>
|
||||
</psalm>
|
||||
"""
|
||||
|
||||
Scenario:
|
||||
Given I have the following code
|
||||
"""
|
||||
<?php
|
||||
class Foo {
|
||||
public function getUrlGenerator(): \Illuminate\Contracts\Routing\UrlGenerator {
|
||||
return url();
|
||||
}
|
||||
}
|
||||
"""
|
||||
When I run Psalm
|
||||
Then I see no errors
|
||||
|
||||
Scenario:
|
||||
Given I have the following code
|
||||
"""
|
||||
<?php
|
||||
class Foo {
|
||||
public function getUrl(): string {
|
||||
return url('example.com');
|
||||
}
|
||||
}
|
||||
"""
|
||||
When I run Psalm
|
||||
Then I see no errors
|
Loading…
Reference in New Issue
Block a user