Skip to content

Add strict_types, type hints and phpcs comments to generated code templates#418

Merged
totten merged 2 commits intototten:masterfrom
jensschuppe:generated-code-style
Oct 31, 2025
Merged

Add strict_types, type hints and phpcs comments to generated code templates#418
totten merged 2 commits intototten:masterfrom
jensschuppe:generated-code-style

Conversation

@jensschuppe
Copy link
Copy Markdown
Contributor

@jensschuppe jensschuppe commented Oct 22, 2025

This adds strict_types declarations, type hints and PHP Code Sniffer ignore comments and final class keywords to relevant generated PHP code that could be subject to QA tool inspections.

Since we're trying to implement https://github.com/systopia/civicrm-extension-template for all of our extensions, this saves a lot of repetitive work.

Ping @dontub for an eye on that.

For entity type definition files this will need a follow-up to totten/php-array-doc.

Copy link
Copy Markdown

@dontub dontub left a comment

Choose a reason for hiding this comment

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

LGTM.

@jensschuppe jensschuppe marked this pull request as ready for review October 23, 2025 10:14
Comment on lines +8 to +10
// phpcs:disable PSR1.Files.SideEffects
require_once '<?php echo $mainFile ?>.civix.php';
// phpcs:enable
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's the purpose of these phpcs comments? civix already writes code that passes all the checks run by the civilint command. Anyone using civilint should be fine without this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The phpcs configuration in systopia/civicrm-extension-template is stricter than civilint and adds this sniff. Since each extension uses this pattern (while not encouraged elsewhere), we find ourselves adding these comments in every extension after civix built the file.

So technically, you're right, but I'd say these comments don't hurt … 🤷

Comment on lines +3 to +6
?>

declare(strict_types = 1);
<?php
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This formatting seems unnecessarily messy. Why not keep the php code together instead of splitting it? E.g. the echo and the $_namespace can all stay within the same code block so we don't need so many extra opening and closing php tags.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agree, this was a quick shot during the sprint and should be simplified.

* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
*/
function <?php echo $mainFile ?>_civicrm_config(&$config): void {
function <?php echo $mainFile ?>_civicrm_config(\CRM_Core_Config $config): void {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Tangential: It's weird that the hook defines $config as pass-by-reference. The $config is so big and central... it's hard for me to imagine what happens if one replaces that object. (My gut says that part of the of hook signature is leftover from the PHP4/PHP5 era, with the rationale lost to the sands of time...)

In any case, it's probably good that this makes it look like a regular object-param. Inviting people to think of it as a reference is... inviting trouble (AFAICS, for no real reason)...

Aside: We should also add the newer ?array $flags = NULL because anyone who actually uses this hook probably should use $flags. (But that's can be a separate issue.)

@totten totten force-pushed the generated-code-style branch from 5fac58f to 4bb7b7e Compare October 31, 2025 20:06
@@ -1,10 +1,11 @@
<?php
echo "<?php\n";
echo "declare(strict_types = 1);\n";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok. JFYI this is the opposite of how I'd have done it. IMO the first file in the diff is cleanest: just put the line in the template, no need for echo.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Yeah, I can see that being a bit shorter. (But 1 SLOC either way.) They're both an improvement.

(N.B. Some files have dynamically-computed parts -- like namespace Foo; or use Foo as E; -- which come immediately after the declare(). When you alternate PHP and plain text, newlines get weirder. This formulation was easy to copy-paste into many places. 🤷 )

@totten
Copy link
Copy Markdown
Owner

totten commented Oct 31, 2025

Tangential notes, for posterity:

  • The question in this PR is whether the templates should nudge people toward stronger type-awareness in new code.
  • On that front, I think it's good. 🚀
  • This should not be interpreted as a categorical judgment that strict_types is always good. It has trade-offs. In PHP, it's optional. That's fine.

@totten totten merged commit 4f12afe into totten:master Oct 31, 2025
1 check passed
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.

4 participants