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

Enabled Strict types and improved type hinting #2

Merged
merged 3 commits into from
Jan 9, 2025

Conversation

Rodeveer
Copy link
Contributor

@Rodeveer Rodeveer commented Jan 6, 2025

  • Added declare(strict-types=1); to ensure that every type is strictly followed.
  • Added missing return types so callables will know what will be returned
  • Reformated code so its more readable with && and || signs.
  • Re-arranged the simplicity of the || checks.
  • Removed intval(...) and did (int) casting. Which performs up to 6 times faster.
  • Returned void for addLinkHeaders since its private and the return is never used.
  • For LinkHeaders.php I've removed the null-operator ?. This could cause an error whenever the null was passed. Now it can't throw an error anymore due to the foreach which was a few lines later.
  • Renamed variable instead of rewriting the variable (bad coding behavior)
  • Tested everything in the test folder to assure everything complies.

…y followed.

- Added missing return types so callables will know what will be returned
- Reformated code so its more readable with && and || signs.
- Re-arranged the simplicity of the || checks.
- Removed intval() and did (int) casting. Which performs up to 6 times faster.
- Returned void for addLinkHeaders since its private and the return is never used.
- For LinkHeaders.php I've removed the null-operator (?). This could cause an error whenever the null was passed. Now it can't throw an error anymore due to the foreach which was a few lines later.
- Renamed variable instead of rewriting the variable (bad coding behavior)
- Tested everything in the test folder to assure everything complies.
Copy link
Member

@indykoning indykoning left a comment

Choose a reason for hiding this comment

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

Thank you for these improvements! I have a few small comments and i'll get the actions running in the meantime 😁

Comment on lines 83 to 90
$value = $matches['value'] ?? '1';

if ($key === 'rel') {
$rel = $value;

continue;
}
$attributes[$key] = $value ?? true;
$attributes[$key] = $value;
Copy link
Member

Choose a reason for hiding this comment

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

In this case this would also impact the rel, adding rel="1" to the headers if the value is empty/not matched, are there any cons to adding the fallback value 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.

You're right. I was tracing the line of how this would work. I've got it at the wrong end. My IDEA mislead me. I'll revert the change! :)

{
$handledHashes = [];

foreach ($this->getLinkProvider()->getLinks() as $link) {
/** @var Link $link */
$hash = md5($link->getHref(), serialize($link->getRels()));
if (! in_array($hash, $handledHashes)) {
$hash = md5($link->getHref(), true); // Previous the second parameter was: serialize($link->getRels()) which gives a string, where the second parameter excepts a boolean.
Copy link
Member

Choose a reason for hiding this comment

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

You're right! This is what it should've been, good catch 🙂

Suggested change
$hash = md5($link->getHref(), true); // Previous the second parameter was: serialize($link->getRels()) which gives a string, where the second parameter excepts a boolean.
$hash = md5($link->getHref() . serialize($link->getRels()));

{
return trim(collect($this->getLinkProvider()->getLinks())
->map([static::class, 'linkToString'])
->map([self::class, 'linkToString'])
Copy link
Member

Choose a reason for hiding this comment

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

My preference here is to use static since if it is ever needed this class could be extended, overriding the linkToString function and then it would call the function on the class extending this one instead of still executing the function of this class itself

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair, I changed the statics to self and then most self back to static since there was no need. Forgotten statement. Also reverting this change, sorry!

@indykoning
Copy link
Member

The pint codestyle action seems to be failing, you should be able to fix that with composer run fix-style 🙂

- Adjusted code based on feedback.
- Ran command to fix-style.
- Added return types to test cases.
@Rodeveer
Copy link
Contributor Author

Rodeveer commented Jan 7, 2025

The pint codestyle action seems to be failing, you should be able to fix that with composer run fix-style 🙂

Nice, done that also. Added the new commit, have a look! :)

@indykoning indykoning changed the title Some minor code-improvements and possible issues with methods Enabled Strict types and improved type hinting Jan 9, 2025
@indykoning indykoning merged commit 375270b into justbetter:master Jan 9, 2025
3 checks passed
@indykoning
Copy link
Member

Awesome! i've merged the changes and will add them to the next release 🙂

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