Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Improve code quality #38

Merged
merged 44 commits into from
Oct 23, 2018
Merged

Conversation

tux-rampage
Copy link
Contributor

@tux-rampage tux-rampage commented Aug 29, 2018

This PR intends to provide the following QA improvements:

  • Increase Test-Coverage (Missing tests)
  • Make resolved type/value injections immutable
    • Setters/Getters are not needed, the whole property name is not needed at all
    • Provide toValue() to obtain the value to inject
  • Create an interface for type/value injection to get rid of too complex instanceof checks
  • Generate PHP 7.1+ code

I will add comments to the diff to clarify my indentations

  • Is this related to quality assurance?

TODOs:

  • Update documentation
  • Add CHANGELOG.md entries

An interface allows more flexibility to implementers/consumers.
The setter/getter for parameter name is polluting the instance's state
and this information is not really needed. It's the job of the resolver
to provide the name/injection link.
@@ -20,6 +28,11 @@
*/
public function setParameterName(string $name) : self
{
trigger_error(
Copy link
Member

Choose a reason for hiding this comment

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

To be documented in CHANGELOG.md

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cahngelog updates will follow. this is still WIP

src/Resolver/AbstractInjection.php Show resolved Hide resolved
public function export() : string;

/**
* @return bool
Copy link
Member

Choose a reason for hiding this comment

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

Not needed

src/Resolver/TypeInjection.php Show resolved Hide resolved
src/Resolver/ValueInjection.php Show resolved Hide resolved
use Zend\Di\CodeGenerator\FactoryInterface;
use ZendTest\Di\TestAsset\InvokableInterface;

final class FactoryProphecy implements FactoryInterface
Copy link
Member

Choose a reason for hiding this comment

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

Urgh, would be better to avoid prophecy at all if it needs to become this complex to please the tool: why do traditional mocks not work here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I don't like it either. Not sure how to replace it. The scenario:

Expect that if the $factories[$key] value is a string, this string is treated as class name and instanciated. But maybe I'm thinking too complex here.

use function is_array;

final class MixedArgumentsFactory implements FactoryInterface
{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is how the output should finally look like. To achieve this I'd remove zend-code. This simplifies the generator, docs and consumers do no longer need zend-code for AoT

This will not trigger a warning as file_put_contents would. Instead
it will throw when opening the file for read access fails.
public function __construct(string $namespace)
{
$this->namespace = $namespace;
}

private function generateAutoloaderClass(array &$classmap)
private function writeFile(string $filename, string $code): void
Copy link
Member

Choose a reason for hiding this comment

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

Only a short notice: space before colon is missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Strange, why didn't the cs-check complain about it? Maybe an outdated version. Thanks for pointing this out. 👍

Copy link
Contributor Author

@tux-rampage tux-rampage Sep 4, 2018

Choose a reason for hiding this comment

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

@froschdesign I ran composer update. The locked version of zend-coding-standard was not out of date. cs-check is still not complaining. Worth a bug report on zendframework/zend-coding-standard? For the sake of consistency I'd argue to leave it this way for the moment and fix this in a separate pull request. Is this OK for you?
(edit: fix link)

@tux-rampage
Copy link
Contributor Author

I've dismissed some minor CS issues that will be handled once ZF CS is applied. Only thing missing here is the documentation of the newly introduced interface

Thanks a lot, @Ocramius

Do you mean the InjectionInterface: https://github.com/zendframework/zend-di/pull/38/files#diff-4ac32a78649ca5bdd8e0ba38b7006a1eR17 ?

What kind of documentation do you have in mind? This is mostly internal and only relevant when somebody wants to implement a custom Resolver. It's not a common use case.

@Ocramius
Copy link
Member

@tux-rampage even if it is internal, so then it should:

  1. be marked as @internal
  2. documented for other maintainers: purpose, behavior, expected usage and expected usage locations

@tux-rampage
Copy link
Contributor Author

@tux-rampage even if it is internal, so then it should:

  1. be marked as @internal
  2. documented for other maintainers: purpose, behavior, expected usage and expected usage locations

@Ocramius, sorry if I expressed this incorrectly, my comment was not about if this should be documented. It was more a question about how.

I hesitate marking this internal. Even though implementing a custom resolver is a rare use case I'd like to give users the freedom with an explicit contract when they know what they're doing. That's the reason why it's not covered in the doc book, yet: It's not something you need to do to get zend-di working and it's something you should consider twice before doing it (i.e. it's better to write a service factory instead of putting additional magic into the auto wiring implementation).

I added some additional PHPDoc. Would that be sufficient for advanced users?

@tux-rampage
Copy link
Contributor Author

@weierophinney I walked through the topics. I thought I had adressed your remarks previously, but I seem to have missed to push them from my notebook. Anyways I made sure this time ;-)

As for your remarks on the templates, these are test covered to ensure specific results. I double checked these expectaions in test/_files/expected-codegen-results/ to address the issues from your review.

I also opened Issue #39 to adress the upcomming coding standard v2. The current one may haunt us with any PR since the tooling does not even complain - which is as frustrating for contributors as for reviewers.

@Ocramius
Copy link
Member

Even though implementing a custom resolver is a rare use case I'd like to give users the freedom with an explicit contract when they know what they're doing.

You said it's internal: if you aren't ready for it to be public API in a release, then mark it as @internal. The marker can be removed later on, once the API is stable enough.

If you don't know exactly how to document it, then it is probably not ready for the wider audience either. Give it your best shot for now. Maybe even rename the methods, if you think that would aid clarity, but don't make it a public interface if it's not ready.

For now this is an option until it's stable enough to become part of
the public api.
@tux-rampage
Copy link
Contributor Author

You said it's internal: if you aren't ready for it to be public API in a release, then mark it as @internal. The marker can be removed later on, once the API is stable enough.

Yes, that sounds like the best way to go, thanks. When I find the time I'll review the resolver parts and check if this has a stable api via interfaces, so that we can cover it an advanced documentation section - when this is the case @internal can be dropped.

If you don't know exactly how to document it, then it is probably not ready for the wider audience either. Give it your best shot for now.

I already added a lot of PHPDoc explaining the purpose of this interface to the maintainers. It now also has the @internal annotation. Could you please have a look if this is sufficient?

Thaks again for your support on this, @Ocramius 👍

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.

Honestly, this looks great! There's some CS consistency, but you have another PR open for that.

As I've noted below, the only thing I think needs changes is the CHANGELOG: you need to elaborate a bit on which contract is being defined, and why, as that's the user-facing aspect.

CHANGELOG.md Outdated
@@ -14,6 +14,15 @@ All notable changes to this project will be documented in this file, in reverse
factory `Zend\Di\Container\GeneratorFactory` for creating a
`Zend\Di\CodeGenerator\InjectorGenerator` instance with zend-servicemanager.

- [#38](https://github.com/zendframework/zend-di/pull/38) adds `Zend\Di\Resolver\InjectionInterface`
to define the contract
Copy link
Member

Choose a reason for hiding this comment

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

Define which contract? Elaborate here, as this is where users get the information they need to determine the impact of the changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, that's related to the discussion above. This will need a change indeed.

Copy link
Member

Choose a reason for hiding this comment

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

Besides this little bit, this is ready to 🚢

CHANGELOG.md Outdated
@@ -14,6 +14,15 @@ All notable changes to this project will be documented in this file, in reverse
factory `Zend\Di\Container\GeneratorFactory` for creating a
`Zend\Di\CodeGenerator\InjectorGenerator` instance with zend-servicemanager.

- [#38](https://github.com/zendframework/zend-di/pull/38) adds `Zend\Di\Resolver\InjectionInterface`
to define the contract
Copy link
Member

Choose a reason for hiding this comment

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

Besides this little bit, this is ready to 🚢

Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

🚢

@Ocramius Ocramius dismissed stale reviews from weierophinney and froschdesign October 23, 2018 09:27

All feedback handled

@tux-rampage
Copy link
Contributor Author

🙌 Thank you all for your support on this. I'm prepping the merge as soon as Travis and coveralls finished.

@Ocramius Ocramius merged commit ea06ffc into zendframework:develop Oct 23, 2018
@Ocramius Ocramius assigned Ocramius and unassigned tux-rampage Oct 23, 2018
@tux-rampage
Copy link
Contributor Author

Thank's @Ocramius - that was quick 😃

@Ocramius
Copy link
Member

Thank YOU for the patience in following this. We should likely decide whether to close off master and do a release, btw.

@tux-rampage
Copy link
Contributor Author

Indeed I was also going to suggest this. I can take care of it to learn how to do it. To do so I would first do the release steps as described in the Maintainers guideline and push it to my repo so one of you can have a look if I did it correctly.

@Ocramius
Copy link
Member

I can verify that later today - remember to sign any tags you create.

@tux-rampage tux-rampage deleted the code-quality branch October 23, 2018 10:49
@tux-rampage
Copy link
Contributor Author

@Ocramius ready: https://github.com/tux-rampage/zend-di - Was this correct?

@Ocramius
Copy link
Member

Hmm, the tag can't be recognised. Did you try uploading your public GPG key?

@tux-rampage
Copy link
Contributor Author

Yes I did. That's strange. To me github shows that it's verified. I had to re-push the tag since I first signed with the wrong key, but that was before my comment here. hold on i'm checking this on my mobile ...
image

@tux-rampage
Copy link
Contributor Author

My mobile phone shows verified as well: https://github.com/tux-rampage/zend-di/tags
Could github simply need some time to sync this through all of their servers?

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

Successfully merging this pull request may close these issues.

4 participants