briannesbitt / Carbon

A simple PHP API extension for DateTime.
https://carbon.nesbot.com/
MIT License
16.56k stars 1.28k forks source link

Problem with /vendor/nesbot/carbon/extension.neon #2817

Closed Temepest74 closed 1 year ago

Temepest74 commented 1 year ago

Hello,

I encountered an issue with the following code: This code cannot be reproduces as a standalone. You need laravel + phpstan as the problem is related with /vendor/nesbot/carbon/extension.neon I post this here because I am unable to find the location of the repo containing that file and I think carbon is at fault, and not phpstan (my other macros works). If this needs to be deleted and moved, please tell me

namespace App\Macros;

use Closure;

class Carbon
{
    public function createWithoutTime(): Closure
    {
        return fn (string $date) => \now()
            ->setDateFrom($date)
            ->setHour(0)
            ->setMinute(0)
            ->setSecond(0)
            ->setMillisecond(0);
    }
}

Carbon version: 2.67.0 PHP version: 8.1.20

The problem: My macro is attached as it should via

        Carbon::mixin(new MacrosCarbon());

Now, this works, I can call my function as I need to. Now, if I don't include

    - ./vendor/nesbot/carbon/extension.neon

inside my phpstan.neon file I get

Call to an undefined static method Illuminate\Support\Carbon::createWithoutTime().

Which makes sense (I think). Now, adding that line of text inside phpstan.neon, I get (verbose output)

Internal error: Internal error: No closure found on line 13 in /var/www/html/app/Macros/Carbon.php in file /var/www/html/app/Http/FileWhereICalledThatFunction.php

     Post the following stack trace to https://github.com/phpstan/phpstan/issues/new?template=Bug_report.yaml:
     #0 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/SourceLocator/Type/ClosureSourceLocator.php(132):
     PHPStan\BetterReflection\SourceLocator\Exception\NoClosureOnLine::create()
     #1 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/SourceLocator/Type/ClosureSourceLocator.php(147):
     PhpParser\NodeVisitorAbstract@anonymous->getClosureNodes()
     #2 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/SourceLocator/Type/ClosureSourceLocator.php(54):
     PHPStan\BetterReflection\SourceLocator\Type\ClosureSourceLocator->getReflectionFunction()
     #3 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/SourceLocator/Type/AggregateSourceLocator.php(26):
     PHPStan\BetterReflection\SourceLocator\Type\ClosureSourceLocator->locateIdentifier()
     #4 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/Reflector/DefaultReflector.php(58):
     PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator->locateIdentifier()
     #5 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/Reflection/ReflectionFunction.php(72):
     PHPStan\BetterReflection\Reflector\DefaultReflector->reflectFunction()
     #6 /var/www/html/vendor/nesbot/carbon/src/Carbon/PHPStan/AbstractMacro.php(130): PHPStan\BetterReflection\Reflection\ReflectionFunction::createFromClosure()
     #7 /var/www/html/vendor/nesbot/carbon/src/Carbon/PHPStan/AbstractMacro.php(87): Carbon\PHPStan\AbstractMacro->getReflectionFunction()
     #8 /var/www/html/vendor/nesbot/carbon/src/Carbon/PHPStan/MacroScanner.php(82): Carbon\PHPStan\AbstractMacro->__construct()
     #9 /var/www/html/vendor/nesbot/carbon/src/Carbon/PHPStan/MacroExtension.php(66): Carbon\PHPStan\MacroScanner->getMethod()
     #10 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/ClassReflection.php(446): Carbon\PHPStan\MacroExtension->getMethod()
     #11 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Type/ObjectType.php(584): PHPStan\Reflection\ClassReflection->getMethod()
     #12 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Type/ObjectType.php(561): PHPStan\Type\ObjectType->getUnresolvedMethodPrototype()
     #13 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Rules/Methods/StaticMethodCallCheck.php(152): PHPStan\Type\ObjectType->getMethod()
     #14 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Rules/Methods/CallStaticMethodsRule.php(43): PHPStan\Rules\Methods\StaticMethodCallCheck->check()
     #15 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php(104): PHPStan\Rules\Methods\CallStaticMethodsRule->processNode()
     #16 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Node/ClassStatementsGatherer.php(98): PHPStan\Analyser\FileAnalyser->PHPStan\Analyser\{closure}()
     #17 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(524): PHPStan\Node\ClassStatementsGatherer->__invoke()
     #18 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(2602): PHPStan\Analyser\NodeScopeResolver::PHPStan\Analyser\{closure}()
     #19 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(1548): PHPStan\Analyser\NodeScopeResolver->callNodeCallbackWithExpression()
     #20 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(2875): PHPStan\Analyser\NodeScopeResolver->processExprNode()
     #21 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(1736): PHPStan\Analyser\NodeScopeResolver->processArgs()
     #22 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(2875): PHPStan\Analyser\NodeScopeResolver->processExprNode()
     #23 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(1612): PHPStan\Analyser\NodeScopeResolver->processArgs()
     #24 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(557): PHPStan\Analyser\NodeScopeResolver->processExprNode()
     #25 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(371): PHPStan\Analyser\NodeScopeResolver->processStmtNode()
     #26 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(542): PHPStan\Analyser\NodeScopeResolver->processStmtNodes()
     #27 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(371): PHPStan\Analyser\NodeScopeResolver->processStmtNode()
     #28 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(620): PHPStan\Analyser\NodeScopeResolver->processStmtNodes()
     #29 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(371): PHPStan\Analyser\NodeScopeResolver->processStmtNode()
     #30 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(592): PHPStan\Analyser\NodeScopeResolver->processStmtNodes()
     #31 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(340): PHPStan\Analyser\NodeScopeResolver->processStmtNode()
     #32 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php(173): PHPStan\Analyser\NodeScopeResolver->processNodes()
     #33 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Command/WorkerCommand.php(130): PHPStan\Analyser\FileAnalyser->analyseFile()
     #34 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97): PHPStan\Command\WorkerCommand->PHPStan\Command\{closure}()
     #35 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/clue/ndjson-react/src/Decoder.php(117): _PHPStan_a540e44a3\Evenement\EventEmitter->emit()
     #36 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97): _PHPStan_a540e44a3\Clue\React\NDJson\Decoder->handleData()
     #37 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/react/stream/src/Util.php(62): _PHPStan_a540e44a3\Evenement\EventEmitter->emit()
     #38 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97):
     _PHPStan_a540e44a3\React\Stream\Util::_PHPStan_a540e44a3\React\Stream\{closure}()
     #39 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/react/stream/src/DuplexResourceStream.php(154): _PHPStan_a540e44a3\Evenement\EventEmitter->emit()
     #40 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/react/event-loop/src/StreamSelectLoop.php(201): _PHPStan_a540e44a3\React\Stream\DuplexResourceStream->handleData()
     #41 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/react/event-loop/src/StreamSelectLoop.php(173): _PHPStan_a540e44a3\React\EventLoop\StreamSelectLoop->waitForStreamActivity()
     #42 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/src/Command/WorkerCommand.php(96): _PHPStan_a540e44a3\React\EventLoop\StreamSelectLoop->run()
     #43 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Command/Command.php(259): PHPStan\Command\WorkerCommand->execute()
     #44 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(870): _PHPStan_a540e44a3\Symfony\Component\Console\Command\Command->run()
     #45 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(261): _PHPStan_a540e44a3\Symfony\Component\Console\Application->doRunCommand()
     #46 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(157): _PHPStan_a540e44a3\Symfony\Component\Console\Application->doRun()
     #47 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(122): _PHPStan_a540e44a3\Symfony\Component\Console\Application->run()
     #48 phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(123): _PHPStan_a540e44a3\{closure}()
     #49 /var/www/html/vendor/phpstan/phpstan/phpstan(8): require('...')
     #50 /var/www/html/vendor/bin/phpstan(119): include('...')
     #51 {main}
kylekatarnls commented 1 year ago

Hello, good news, this has been fixed in PHPStan 1.11.x-dev (you can composer require phpstan/phpstan:1.11.x-dev to fix this issue.