Add strict types for all php files#383
Add strict types for all php files#383neenjaw merged 8 commits intoexercism:mainfrom codedge:#352-strict-types
Conversation
|
If this is WIP can we make it a draft PR? Looks good so far, just needs the explanatory message |
|
Yes, please make it a draft. I know there are some things open and I'll ping you when I think it is ready. |
|
@neenjaw I feel like the PR is ready to be checked. All changes should be explained in my comment above, I hope 😉 |
|
Cool, I'll check it out later today |
neenjaw
left a comment
There was a problem hiding this comment.
One small comment about the formatting of the paragraph. Looks great otherwise. Tell me what you think and then I can merge?
| * By adding type hints and enabling strict type checking, code can become easier to read, | ||
| * self-documenting and reduce the number of potential bugs. | ||
| * By default, type declarations are non-strict, | ||
| * which means they will attempt to change the original type to match the type specified by the type-declaration. | ||
| * In other words, if you pass a string to a function requiring a float, | ||
| * it will attempt to convert the string value to a float. | ||
| * To enable strict mode, a single declare directive must be placed at the top of the file. | ||
| * This means that the strictness of typing is configured on a per-file basis. | ||
| * This directive not only affects the type declarations of parameters, but also a function's return type. |
There was a problem hiding this comment.
Is there any chance we can format this paragraph differently? It seems strange that each line is so different in length. Maybe aim for a line length of 80? The content itself is fine. :)
|
Yes, good idea to go for the 80 chars. I am gonna change it and then you can merge. |
|
@neenjaw I tried to justify the text a bit more but it is hard to not "destroy" the sentence with unneeded line breaks. Do you think it is ok so? |
| /* | ||
| * By adding type hints and enabling strict type checking, code can become easier to read, | ||
| * self-documenting and reduce the number of potential bugs. | ||
| * By default, type declarations are non-strict, which means they will attempt to | ||
| * change the original type to match the type specified by the type-declaration. | ||
| * In other words, if you pass a string to a function requiring a float, | ||
| * it will attempt to convert the string value to a float. | ||
| * To enable strict mode, a single declare directive must be placed at the top of the file. | ||
| * This means that the strictness of typing is configured on a per-file basis. | ||
| * This directive not only affects the type declarations of parameters, but also a function's | ||
| * return type. | ||
| * | ||
| * For more info review the Concept on strict type checking in the PHP track <link>. | ||
| * To disable strict typing, comment out the directive below. | ||
| */ |
There was a problem hiding this comment.
| /* | |
| * By adding type hints and enabling strict type checking, code can become easier to read, | |
| * self-documenting and reduce the number of potential bugs. | |
| * By default, type declarations are non-strict, which means they will attempt to | |
| * change the original type to match the type specified by the type-declaration. | |
| * In other words, if you pass a string to a function requiring a float, | |
| * it will attempt to convert the string value to a float. | |
| * To enable strict mode, a single declare directive must be placed at the top of the file. | |
| * This means that the strictness of typing is configured on a per-file basis. | |
| * This directive not only affects the type declarations of parameters, but also a function's | |
| * return type. | |
| * | |
| * For more info review the Concept on strict type checking in the PHP track <link>. | |
| * To disable strict typing, comment out the directive below. | |
| */ | |
| /* | |
| * By adding type hints and enabling strict type checking, code can become | |
| * easier to read, self-documenting and reduce the number of potential bugs. | |
| * By default, type declarations are non-strict, which means they will attempt | |
| * to change the original type to match the type specified by the | |
| * type-declaration. In other words, if you pass a string to a function | |
| * requiring a float, it will attempt to convert the string value to a float. | |
| * | |
| * To enable strict mode, a single declare directive must be placed at the top | |
| * of the file. This means that the strictness of typing is configured on a | |
| * per-file basis. This directive not only affects the type declarations of | |
| * parameters, but also a function's return type. | |
| * | |
| * For more info review the Concept on strict type checking in the PHP track | |
| * <link>. | |
| * | |
| * To disable strict typing, comment out the directive below. | |
| */ |
This would be a strict 80 width adaptation.
| /* | ||
| * By adding type hints and enabling strict type checking, code can become easier to read, | ||
| * self-documenting and reduce the number of potential bugs. | ||
| * By default, type declarations are non-strict, which means they will attempt to | ||
| * change the original type to match the type specified by the type-declaration. | ||
| * In other words, if you pass a string to a function requiring a float, | ||
| * it will attempt to convert the string value to a float. | ||
| * To enable strict mode, a single declare directive must be placed at the top of the file. | ||
| * This means that the strictness of typing is configured on a per-file basis. | ||
| * This directive not only affects the type declarations of parameters, but also a function's | ||
| * return type. | ||
| * | ||
| * For more info review the Concept on strict type checking in the PHP track <link>. | ||
| * To disable strict typing, comment out the directive below. | ||
| */ |
There was a problem hiding this comment.
| /* | |
| * By adding type hints and enabling strict type checking, code can become easier to read, | |
| * self-documenting and reduce the number of potential bugs. | |
| * By default, type declarations are non-strict, which means they will attempt to | |
| * change the original type to match the type specified by the type-declaration. | |
| * In other words, if you pass a string to a function requiring a float, | |
| * it will attempt to convert the string value to a float. | |
| * To enable strict mode, a single declare directive must be placed at the top of the file. | |
| * This means that the strictness of typing is configured on a per-file basis. | |
| * This directive not only affects the type declarations of parameters, but also a function's | |
| * return type. | |
| * | |
| * For more info review the Concept on strict type checking in the PHP track <link>. | |
| * To disable strict typing, comment out the directive below. | |
| */ | |
| /* | |
| * By adding type hints and enabling strict type checking, code can become | |
| * easier to read, self-documenting and reduce the number of potential bugs. | |
| * By default, type declarations are non-strict, which means they will attempt | |
| * to change the original type to match the type specified by the | |
| * type-declaration. | |
| * | |
| * In other words, if you pass a string to a function requiring a float, | |
| * it will attempt to convert the string value to a float. | |
| * | |
| * To enable strict mode, a single declare directive must be placed at the top | |
| * of the file. | |
| * This means that the strictness of typing is configured on a per-file basis. | |
| * This directive not only affects the type declarations of parameters, but also | |
| * a function's return type. | |
| * | |
| * For more info review the Concept on strict type checking in the PHP track | |
| * <link>. | |
| * | |
| * To disable strict typing, comment out the directive below. | |
| */ |
Here is a strict 80 width adaptation based on your line breaks.
There was a problem hiding this comment.
I've also tried 120 chars width, but that looks really bad.
There was a problem hiding this comment.
(I recommend copying and pasting it in your IDE if you want to preview how it looks)
|
Thanks @SleeplessByte, I think one of those formats would be great. @codedge, which formatting do you think works best for this use? |
|
I change the text to match the second suggestion. Looks better now? |
|
Looks great, let's merge it |
The PR to add strict types to all PHP files (exercism#383) broke the tests for this assignment because the months are all sent as strings. This PR fixes the tests by changing the months to ints.
Resolve #352
This PR adds
declare(strict_types=1)to all php files and an explanation what it means.The sentences of the explanation are sometimes a bit long to match the 120 chars limit that is enforces with the PSR-2 standard. I therefore made some line breaks to meet this requirement.
Changes
Rule to enfore strict types
An additional phpcs rule enforces a proper declaration of strict types for each file. The rule
SlevomatCodingStandard.TypeHints.DeclareStrictTypesfromslevomat/coding-standardstakes care of this.Rule to enforce explanation of strict type
I've added a custom phpcs rule, that takes care of the explanation. I think this will make it easier to apply coding standards in the future.
Using composer for lint check
To properly include the custom rules, composer is used to run the check in Github actions.
Specifying php version
To properly specify the version used to CI action, the key
php-versionneeds to be used, instead ofversion. Seeshivammathur/setup-php