Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add return types to closures #9

Merged
merged 8 commits into from Feb 21, 2022

Conversation

alexander-schranz
Copy link

@alexander-schranz alexander-schranz commented Feb 18, 2022

Add return types to closures via @rectorphp to your composer#10547 PR.

rector.php
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector;
use Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector;
use Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
    $parameters = $containerConfigurator->parameters();
    $services = $containerConfigurator->services();

    $parameters->set(Option::PATHS, [__DIR__ . '/src']); // only tests directory
    $parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, __DIR__ . '/phpstan/config.neon');

    // basic rules
    // $parameters->set(Option::AUTO_IMPORT_NAMES, true);
    $parameters->set(Option::IMPORT_DOC_BLOCKS, true);
    $parameters->set(Option::IMPORT_SHORT_CLASSES, false);

    $services->set(AddClosureReturnTypeRector::class);
    
    // following not enabled yet:
    // $services->set(AddReturnTypeDeclarationRector::class);
    // $services->set(AddVoidReturnTypeWhereNoReturnRector::class);
    // $services->set(ReturnTypeFromReturnNewRector::class);
    // $services->set(ReturnTypeFromStrictTypedCallRector::class);
    // $services->set(ReturnTypeFromStrictTypedPropertyRector::class);

    $services->set(ReturnTypeDeclarationRector::class);
};

CI currently fails with one little phpstan issue: alexander-schranz#2

Not sure about return true here should here not be an array returned?

https://github.com/composer/composer/blob/32852304d0c8823fe535dfc5b19e68952d4a6f41/src/Composer/Repository/ComposerRepository.php#L1509

I did also run a full rector rules for adding return types but that is failing: alexander-schranz#1
But still maybe it could help in some cases to check where maybe something can still be added.

@Seldaek
Copy link
Owner

Seldaek commented Feb 19, 2022

Thanks for the PR! The return true in ComposerRepository is to indicate the cache is fresh and can be read, but I guess we could perhaps read the cache in there and return the content instead of true. I'll take a closer look when merging.

@Seldaek Seldaek merged commit 1321bfc into Seldaek:add_returns Feb 21, 2022
@Seldaek
Copy link
Owner

Seldaek commented Feb 21, 2022

Sorry for the error/commit spam, but anyway thanks again :)

@alexander-schranz
Copy link
Author

@Seldaek No problem. Happy the PRs could be used by you.

@alexander-schranz alexander-schranz deleted the add_returns_closures branch February 21, 2022 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants