Skip to content

[PHP] Add declareStrictTypes option to AbstractPhpCodegen to generate declare(strict_types=1); #21179

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
protected String interfaceNamePrefix = "", interfaceNameSuffix = "Interface";
protected String abstractNamePrefix = "Abstract", abstractNameSuffix = "";
protected String traitNamePrefix = "", traitNameSuffix = "Trait";
protected boolean declareStrictTypes = false;

private Map<String, String> schemaKeyToModelNameCache = new HashMap<>();

Expand Down Expand Up @@ -285,6 +286,11 @@ public void processOpts() {

// all PHP codegens requires Composer, it means that we need to exclude from SVN at least vendor folder
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));
// Add declareStrictTypes property to the additionalProperties
if (additionalProperties.containsKey("declareStrictTypes")) {
this.declareStrictTypes = Boolean.parseBoolean(additionalProperties.get("declareStrictTypes").toString());
}
additionalProperties.put("declareStrictTypes", declareStrictTypes);
}

public String toSrcPath(final String packageName, final String basePath) {
Expand Down Expand Up @@ -567,6 +573,14 @@ public String toInterfaceName(final String name) {
public String toAbstractName(final String name) {
return camelize(abstractNamePrefix + name + abstractNameSuffix);
}

@Override
public String getHelp() {
return "Generates a PHP client library." + "\n" +
"Options:\n" +
" - declareStrictTypes: boolean, set to true to add declare(strict_types=1); in generated PHP files.";
}


/**
* Output the proper trait name (capitalized).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
{{#declareStrictTypes}}declare(strict_types=1);{{/declareStrictTypes}}
Copy link
Member

Choose a reason for hiding this comment

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

better do in the following way

{{#declareStrictTypes}}
declare(strict_types=1);
{{/declareStrictTypes}}

to avoid blank lines

Copy link
Author

Choose a reason for hiding this comment

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

Sure, I'll do it.


/**
* ApiException
* PHP version 8.1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
{{#declareStrictTypes}}declare(strict_types=1);{{/declareStrictTypes}}
/**
* Configuration
* PHP version 8.1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
{{#declareStrictTypes}}declare(strict_types=1);{{/declareStrictTypes}}

/**
* FormDataProcessor
* PHP version 7.4
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
{{#declareStrictTypes}}declare(strict_types=1);{{/declareStrictTypes}}
/**
* HeaderSelector
* PHP version 8.1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
{{#declareStrictTypes}}declare(strict_types=1);{{/declareStrictTypes}}
/**
* ModelInterface
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
{{#declareStrictTypes}}declare(strict_types=1);{{/declareStrictTypes}}
/**
* ObjectSerializer
*
Expand Down
Loading