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

How to correctly returnType array of arrays? #31

Closed
lowtower opened this issue Aug 10, 2018 · 2 comments
Closed

How to correctly returnType array of arrays? #31

lowtower opened this issue Aug 10, 2018 · 2 comments

Comments

@lowtower
Copy link

I have a method that returns an array something like this (from an zend-expressive project):

    /**
     * Returns the container dependencies
     *
     * @return string[]
     */
    public function getDependencies() : array
    {
        return [
            'invokables' => [],
            'factories' => [
                Handler\Handler::class => Handler\HandlerFactory::class,
            ],
        ];
    }

    /**
     * Returns the templates configuration
     *
     * @return string[]
     */
    public function getTemplates() : array
    {
        return [
            'paths' => [
                'test' => [__DIR__ . '/../templates/'],
            ],
        ];
    }

The returnType like above returns a phpstan error:

method getDependencies() should return array but returns array<string, array<string,string>>.
method getTemplates() should return array but returns array<string, array<string, array<int, string>>>.

How would I correctly type hint these arrays?

Thanks in advance

@lowtower
Copy link
Author

Found the answer myself

<?php

declare(strict_types=1);

namespace Album;

/**
 * The configuration provider for the Album module
 *
 * @see https://docs.zendframework.com/zend-component-installer/
 */
class ConfigProvider
{
    /**
     * Returns the configuration array
     *
     * To add a bit of a structure, each section is defined in a separate
     * method which returns an array with its configuration.
     */
    public function __invoke() : array
    {
        return [
            'dependencies' => $this->getDependencies(),
            'templates' => $this->getTemplates(),
        ];
    }

    /**
     * Returns the container dependencies
     *
     * @return array<string, array>
     */
    private function getDependencies() : array
    {
        return [
            'invokables' => [],
            'factories' => [],
        ];
    }

    /**
     * Returns the templates configuration
     *
     * @return array<string, array>
     */
    private function getTemplates() : array
    {
        return [
            'paths' => [
                'album' => [__DIR__ . '/../templates/'],
            ],
        ];
    }
}

Still I wonder about the error message, especially regarding the proposal for getTemplates().

Cheers,
LT.

@lowtower
Copy link
Author

Just for the record:
phpstan seems to support these kind of docBlocks while phpcs does not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant