Skip to content

Commit

Permalink
[TypeDeclaration] Handle crash on @mixin usage on ReturnTypeFromStric…
Browse files Browse the repository at this point in the history
…tTypedCallRector (#5823)

* [TypeDeclaration] Handle with @mixin on ReturnTypeFromStrictTypedCallRector

* Fix
  • Loading branch information
samsonasik committed Apr 17, 2024
1 parent 2b811af commit 8cf4340
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
6 changes: 4 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,10 @@ parameters:
- '#Parameter \#1 \$suffix of method PHPUnit\\Framework\\Assert\:\:assertStringEndsWith\(\) expects non\-empty\-string, string given#'

-
message: '#Function "function_exists\(\)" cannot be used/left in the code#'
path: src/functions/node_helper.php
message: '#Function "(function|method)_exists\(\)" cannot be used/left in the code#'
paths:
- src/functions/node_helper.php
- src/DependencyInjection/LazyContainerFactory.php

-
message: '#Function "dump_node\(\)" cannot be used/left in the code\: #'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\FixturePhp80;

// future note: this 2 classes here on purpose to reproduce crash
// see https://github.com/rectorphp/rector/issues/8600

/**
* @method static mixed get(array|string $key, mixed|\Closure $default = null)
*
* @mixin \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\Source\Repository
*/
class Cache
{
}

class WithMixin
{
/**
* @throws GuzzleException
* @throws JsonException
*/
protected function getOauthToken(string $key)
{
return Cache::get($key);
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\FixturePhp80;

// future note: this 2 classes here on purpose to reproduce crash
// see https://github.com/rectorphp/rector/issues/8600

/**
* @method static mixed get(array|string $key, mixed|\Closure $default = null)
*
* @mixin \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\Source\Repository
*/
class Cache
{
}

class WithMixin
{
/**
* @throws GuzzleException
* @throws JsonException
*/
protected function getOauthToken(string $key): mixed
{
return Cache::get($key);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\Source;

class Repository
{
/**
* @template TCacheValue
* @return (TCacheValue is null ? mixed : TCacheValue)
*/
public function get($key, $default = null): mixed
{
}
}
13 changes: 13 additions & 0 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,19 @@ static function (NameScopeFactory $nameScopeFactory, Container $container): void
}
);

$rectorConfig->afterResolving(
TypeMapperInterface::class,
static function (TypeMapperInterface $typeMapper, Container $container): void {
if (! method_exists($typeMapper, 'autowire')) {
return;
}

$typeMapper->autowire(
$container->make(PHPStanStaticTypeMapper::class),
);
}
);

$rectorConfig->afterResolving(
ArrayTypeMapper::class,
static function (ArrayTypeMapper $arrayTypeMapper, Container $container): void {
Expand Down

0 comments on commit 8cf4340

Please sign in to comment.