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

✨ method parameter default value rendering #374

Merged
merged 10 commits into from
Nov 4, 2024

Conversation

GautierDele
Copy link
Contributor

For now the default value for method parameter is not used but present, when rendering, default value is never used so it can lead to misleading behavior where your IDE tells you to fill a value but it has a default one so it's not needed
image

In this PR I added the ability for method parameter to implement this

@GautierDele
Copy link
Contributor Author

@jaapio don't hesitate i'm available if this PR needs something 😉

@jaapio
Copy link
Member

jaapio commented Oct 27, 2024

Thanks for your contribution. It looks like phpstan has spot some issues that make the test fail.

Could you please have a look at that?

@GautierDele
Copy link
Contributor Author

Should be fixed as verified with phpstan playground

Copy link
Member

@jaapio jaapio left a comment

Choose a reason for hiding this comment

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

Thanks for fixing the code style. I did now check the code itself. And I have my doubts about how good it is. The basic cases are covered, but more - complex might not work. It would be good to prove that it works with some extra tests.

$parameterDefaultValueStr = null;
if ($parameter->getDefaultValue() !== null) {
$parameterDefaultValueStr = $parameter->getDefaultValue();
settype($parameterDefaultValueStr, (string)$parameter->getType());
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem to be correct to me. Default values can be complex definitions.
It might be good to check with a number of testcases to ensure this works as expected.

Please do also add a testcase with a custom class.

Copy link
Contributor Author

@GautierDele GautierDele Oct 27, 2024

Choose a reason for hiding this comment

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

You mean isolate the test in a test class dedicated to this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh no i understood, give me some time 😄

@GautierDele
Copy link
Contributor Author

You were right, it didn't cover enough cases, I did rewrote the concept, what do you think 😊

@GautierDele
Copy link
Contributor Author

@jaapio did you have any time looking at it maybe ? 😄

Copy link
Member

@jaapio jaapio left a comment

Choose a reason for hiding this comment

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

In general this looks very nice. I have a few requests to change the implementation that will help me to keep this working in the future and allow me to improve. Those requests might sound strange, but they are based on years of experience with this codebase. I try to keep things as closed as possible, to allow me to do major refactorings when needed without breaking changes.

Regarding these breaking changes I do have a few doubts. We are changing the return type of the getDefaultValue method. I do get that we need to change the type of the property which can be code as we stretch the accepted values. Maybe we should use the property directly in the __toString method as that will give us the information we need. But we can keep the return value of getDefaultValue a string?

use function str_repeat;
use function strlen;

class MethodParameterFactory
Copy link
Member

Choose a reason for hiding this comment

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

Please mark this class as final and @internal as we should not make this part of the backward compatibility promise. Extending this class should be blocked as it is an internal only class. Maybe this should even be part of the Method tag? Unless there is a reason to extract this because you want to share this class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All good

For me, extracting this logic from the base code is a good idea for the maintanability
It could have been a trait but I did get inspired by the package way of working, could be wrong

return '';
}

protected function formatDouble(float $defaultValue): string
Copy link
Member

Choose a reason for hiding this comment

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

I do prefer private for all new methods as we do not want others to interfere with this code. This make it easier to refactor methods as private methods are internal only, and we can remove them at will.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All good

@GautierDele
Copy link
Contributor Author

image

I'm not quite sure what's going on here, if you have an idea, don't hesitate 😊

@GautierDele
Copy link
Contributor Author

All good I figured it out

Copy link
Member

@jaapio jaapio left a comment

Choose a reason for hiding this comment

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

We are nearly there, just a few minor changes and than we can merge this. Thanks a lot for all your work so far.

@@ -60,8 +69,17 @@ public function isVariadic(): bool
return $this->isVariadic;
}

public function getDefaultValue(): ?string
public function getDefaultValue(): mixed
Copy link
Member

Choose a reason for hiding this comment

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

Can we please revert this as I did notice before, because I see this as a backward compatibility break. I think we should not do that right now. This will also solve the failing tests

}

/**
* @param array<array, null, int, float, bool, string, object> $defaultValue
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @param array<array, null, int, float, bool, string, object> $defaultValue
* @param array<array|null|int|float|bool|string|object> $defaultValue

@GautierDele
Copy link
Contributor Author

Seems all good

@GautierDele
Copy link
Contributor Author

I added the fact that it returns "null" when no value was set as before

@jaapio jaapio merged commit 6fc32d3 into phpDocumentor:5.x Nov 4, 2024
22 of 25 checks passed
@jaapio
Copy link
Member

jaapio commented Nov 4, 2024

Thanks again for your contribution!

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

Successfully merging this pull request may close these issues.

2 participants