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

Refined ServiceManager factory, delegator and configuration types, to allow for easier introspection of mis-configuration at type level #98

Merged
merged 20 commits into from
Sep 18, 2021

Conversation

boesing
Copy link
Member

@boesing boesing commented Jul 31, 2021

Q A
Documentation kinda
QA yes

Description

This reduces the overall baseline by 140 errors. Some errors still have to be suppressed inline.

src/Config.php Show resolved Hide resolved
src/ServiceManager.php Outdated Show resolved Hide resolved
@boesing boesing force-pushed the feature/psalm-types branch from 1905064 to 3dcb25c Compare July 31, 2021 20:28
src/ServiceManager.php Outdated Show resolved Hide resolved
src/ConfigInterface.php Outdated Show resolved Hide resolved
Copy link
Member

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a GREAT addition! I've flagged a few nitpicks, which are primarily to close a few more Psalm errors as well as simplify some of the methods by promoting early returns.

Otherwise, 🚢

src/AbstractPluginManager.php Show resolved Hide resolved
src/Config.php Show resolved Hide resolved
src/ConfigInterface.php Outdated Show resolved Hide resolved
src/ConfigInterface.php Outdated Show resolved Hide resolved
src/ServiceManager.php Outdated Show resolved Hide resolved
src/ServiceManager.php Show resolved Hide resolved
src/ServiceManager.php Outdated Show resolved Hide resolved
test/AbstractPluginManagerTest.php Outdated Show resolved Hide resolved
test/CommonServiceLocatorBehaviorsTrait.php Outdated Show resolved Hide resolved
test/CommonServiceLocatorBehaviorsTrait.php Outdated Show resolved Hide resolved
@Ocramius Ocramius changed the base branch from 3.8.x to 3.9.x September 14, 2021 03:44
@Ocramius Ocramius added this to the 3.9.0 milestone Sep 14, 2021
@boesing boesing force-pushed the feature/psalm-types branch 2 times, most recently from 40222ff to 1cf60f2 Compare September 14, 2021 18:39
@boesing boesing force-pushed the feature/psalm-types branch from 1cf60f2 to 69bc237 Compare September 14, 2021 18:41
@boesing
Copy link
Member Author

boesing commented Sep 15, 2021

After some talks to @Ocramius, we are thinking about extracting the @template-Annotation into a dedicated patch to avoid upstream analysis errors.

The dedicated patch would target v4.0 which does not have any ETA and which would require a huge amount of work in a majority of our components. Maybe this is something for the hacktoberfest?

src/AbstractPluginManager.php Outdated Show resolved Hide resolved
src/Config.php Show resolved Hide resolved
src/ConfigInterface.php Outdated Show resolved Hide resolved
src/ConfigInterface.php Outdated Show resolved Hide resolved
@@ -23,6 +23,7 @@ interface DelegatorFactoryInterface
* A factory that creates delegates of a given service
*
* @param string $name
* @psalm-param callable():mixed $callback
* @param null|array $options
* @return object
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is supposed to return object, then $callback cannot return mixed.

I feel like the DelegatorFactoryInterface may benefit from a templated type, in future (not here, due to BC)

Copy link
Member Author

@boesing boesing Sep 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since ContainerInterface#get can return mixed, this actually has to be mixed :-(

But yes, having a templated type would help for sure but until then, I've thought that mixed would be fine until the return type is object 🤷🏼‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but the commented line is about @return object (wrong)

Copy link
Member Author

@boesing boesing Sep 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I can follow.

Do you want me to change existing documentation that we end up having bc breaks? as of now, due to the lack of typing, callable factories can return mixed. When I enforce object now, this will be pain probably. Thats why I think that might be a BC break but I am not 100% sure tho.

Or do you want me to open up the return value of the delegator to be mixed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I am fine with both as I don't have that much experience with adding psalm types on such a widely used component. As always, I trust your experience. :-)

src/ServiceManager.php Outdated Show resolved Hide resolved
src/ServiceManager.php Outdated Show resolved Hide resolved
test/ConfigTest.php Outdated Show resolved Hide resolved
test/ContainerTest.php Show resolved Hide resolved
test/ServiceManagerTest.php Outdated Show resolved Hide resolved
This reduces the overall baseline by 140 errors. Some errors still have
to be suppressed inline.

Signed-off-by: Maximilian Bösing <[email protected]>
Signed-off-by: Maximilian Bösing <[email protected]>
…`factories` config entries

Signed-off-by: Maximilian Bösing <[email protected]>
Signed-off-by: Maximilian Bösing <[email protected]>
This will prevent introducing BC breaks while still provide a soft return type.

Signed-off-by: Maximilian Bösing <[email protected]>
This will ensure the test does exactly what it supposed to do.

Signed-off-by: Maximilian Bösing <[email protected]>
@boesing boesing force-pushed the feature/psalm-types branch from 69bc237 to b906701 Compare September 17, 2021 21:46
@boesing boesing requested a review from Ocramius September 17, 2021 21:46
src/AbstractPluginManager.php Show resolved Hide resolved
src/AbstractPluginManager.php Outdated Show resolved Hide resolved
src/Config.php Show resolved Hide resolved
@@ -66,6 +67,8 @@ public function __construct(array $config = [])
unset($config[$key]);
}
}

/** @psalm-suppress ArgumentTypeCoercion */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This coercion shouldn't apply anymore: merge() has the correct types? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArrayUtils::merge returns array and thus, psalm loses track.

Not sure if something like this would help psalm to keep track:

/**
 * @template TArray1 of array
 * @template TArray2 of array
 * @param TArray1 $a
 * @param TArray2 $b
 * @return TArray1&TArray2
 */

But this wont properly work for nested arrays (as they are here). There is absolutely no way but to trust that ArrayUtils::merge will return the same types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but you are not calling ArrayUtils::merge(), but rather $this->merge(), which is well typed, and has its own suppressions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave this alone for now - I think it's not worth wasting effort on it after all the work you've already put in this, and it's in the internals anyway :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added reportUnusedPsalmSuppress in the config and thats not reported so something might still be odd :-/

@@ -23,6 +23,7 @@ interface DelegatorFactoryInterface
* A factory that creates delegates of a given service
*
* @param string $name
* @psalm-param callable():mixed $callback
* @param null|array $options
* @return object
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but the commented line is about @return object (wrong)

src/PluginManagerInterface.php Outdated Show resolved Hide resolved
src/ServiceManager.php Show resolved Hide resolved
test/ContainerTest.php Show resolved Hide resolved
@boesing boesing force-pushed the feature/psalm-types branch from e175bb9 to 883ff57 Compare September 18, 2021 13:07
@boesing boesing requested a review from Ocramius September 18, 2021 13:29
@@ -66,6 +67,8 @@ public function __construct(array $config = [])
unset($config[$key]);
}
}

/** @psalm-suppress ArgumentTypeCoercion */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave this alone for now - I think it's not worth wasting effort on it after all the work you've already put in this, and it's in the internals anyway :)

@@ -20,16 +21,23 @@
use function get_class;

/**
* @see ConfigInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like how you used @see to import types that are otherwise elided by the CS tooling :D

@Ocramius Ocramius self-assigned this Sep 18, 2021
@Ocramius Ocramius dismissed weierophinney’s stale review September 18, 2021 18:09

Dismissing feedback after multiple iterations highlighted a massive improvement in type detail/edge cases :)

@Ocramius Ocramius changed the title Introduce plenty of psalm types Refined ServiceManager factory, delegator and configuration types, to allow for easier introspection of mis-configuration at type level Sep 18, 2021
@Ocramius Ocramius merged commit eda81f9 into laminas:3.9.x Sep 18, 2021
@Ocramius
Copy link
Member

🚢

will also release ASAP - it's a huge improvement :)

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

Successfully merging this pull request may close these issues.

3 participants