Skip to content

Commit

Permalink
Fix new PHPCompatibility-dev sniff
Browse files Browse the repository at this point in the history
PHPCompatibility, in their dev branch, just added a sniff for the
`$escape` parameter to various CSV-related functions, since PHP has
decided they might change the default in 9.0.
  • Loading branch information
anomiex committed Oct 7, 2024
1 parent 3f185ff commit a9280a2
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 15 deletions.
2 changes: 2 additions & 0 deletions projects/packages/codesniffer/Jetpack-Compat-74/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
<exclude name="PHPCompatibility.ParameterValues.NewIDNVariantDefault.NotSet" />
<exclude name="PHPCompatibility.ParameterValues.NewProcOpenCmdArray.Found" />
<exclude name="PHPCompatibility.ParameterValues.NewStripTagsAllowableTagsArray.Found" />
<exclude name="PHPCompatibility.ParameterValues.RemovedProprietaryCSVEscaping.ChangedBehaviour" />
<exclude name="PHPCompatibility.ParameterValues.RemovedProprietaryCSVEscaping.EmptyStringNotAllowed" />
<exclude name="PHPCompatibility.Syntax.NewArrayUnpacking.Found" />
</rule>
</ruleset>
25 changes: 25 additions & 0 deletions projects/packages/codesniffer/Jetpack-Compat-80/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,40 @@
<!-- This standard is automatically generated by build-compat-rulesets.sh -->

<rule ref="Jetpack-Compat-74">
<exclude name="PHPCompatibility.Attributes.NewAttributes.DoctrineORMAttributeFound" />
<exclude name="PHPCompatibility.Attributes.NewAttributes.Found" />
<exclude name="PHPCompatibility.Attributes.NewAttributes.FoundCloseTag" />
<exclude name="PHPCompatibility.Attributes.NewAttributes.FoundInline" />
<exclude name="PHPCompatibility.Attributes.NewAttributes.FoundMultiLine" />
<exclude name="PHPCompatibility.Attributes.NewAttributes.PHPNativeAttributeFound" />
<exclude name="PHPCompatibility.Attributes.NewAttributes.PHPStormAttributeFound" />
<exclude name="PHPCompatibility.Attributes.NewAttributes.PHPUnitAttributeFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.addressinfoFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.attributeFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.curlhandleFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.curlmultihandleFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.curlsharehandleFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.deflatecontextFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.enchantbrokerFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.enchantdictionaryFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.gdimageFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.inflatecontextFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.ocicollectionFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.ocilobFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.opensslasymmetrickeyFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.opensslcertificateFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.opensslcertificatesigningrequestFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.phptokenFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.reflectionuniontypeFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.shmopFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.socketFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.sysvmessagequeueFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.sysvsemaphoreFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.sysvsharedmemoryFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.unhandledmatcherrorFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.valueerrorFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.weakmapFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.xmlparserFound" />
<exclude name="PHPCompatibility.Classes.NewConstructorPropertyPromotion.Found" />
<exclude name="PHPCompatibility.Constants.NewConstants.filter_validate_boolFound" />
<exclude name="PHPCompatibility.Constants.NewConstants.libenchant_versionFound" />
Expand Down
11 changes: 11 additions & 0 deletions projects/packages/codesniffer/Jetpack-Compat-81/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@
<exclude name="PHPCompatibility.Classes.NewClasses.curlstringfileFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.fibererrorFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.fiberFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.ftp_connectionFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.gdfontFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.imap_connectionFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.intldatepatterngeneratorFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.ldap_connectionFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.ldap_resultentryFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.ldap_resultFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.pgsql_connectionFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.pgsql_lobFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.pgsql_resultFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.pspell_configFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.pspell_dictionaryFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.reflectionenumbackedcaseFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.reflectionenumFound" />
<exclude name="PHPCompatibility.Classes.NewClasses.reflectionenumunitcaseFound" />
Expand Down
2 changes: 2 additions & 0 deletions projects/packages/codesniffer/Jetpack-Compat-82/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@
<exclude name="PHPCompatibility.Constants.NewConstants.tcp_keepidleFound" />
<exclude name="PHPCompatibility.Constants.NewConstants.tcp_keepintvlFound" />
<exclude name="PHPCompatibility.Constants.NewConstants.tcp_notsent_lowatFound" />
<exclude name="PHPCompatibility.FunctionDeclarations.NewParamTypeDeclarations.DNFTypeFound" />
<exclude name="PHPCompatibility.FunctionDeclarations.NewParamTypeDeclarations.NonUnionFalse" />
<exclude name="PHPCompatibility.FunctionDeclarations.NewParamTypeDeclarations.NonUnionNull" />
<exclude name="PHPCompatibility.FunctionDeclarations.NewParamTypeDeclarations.trueFound" />
<exclude name="PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.DNFTypeFound" />
<exclude name="PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.NonUnionFalse" />
<exclude name="PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.NonUnionNull" />
<exclude name="PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.trueFound" />
Expand Down
9 changes: 1 addition & 8 deletions projects/packages/codesniffer/Jetpack-Compat-83/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<!-- This standard is automatically generated by build-compat-rulesets.sh -->

<rule ref="Jetpack-Compat-82">
<exclude name="PHPCompatibility.Classes.NewReadonlyClasses.AnonClass" />
<exclude name="PHPCompatibility.Constants.NewConstants.af_divertFound" />
<exclude name="PHPCompatibility.Constants.NewConstants.curlinfo_cainfoFound" />
<exclude name="PHPCompatibility.Constants.NewConstants.curlinfo_capathFound" />
Expand Down Expand Up @@ -34,10 +35,6 @@
<exclude name="PHPCompatibility.Constants.NewConstants.openssl_cms_oldmimetypeFound" />
<exclude name="PHPCompatibility.Constants.NewConstants.pdo_odbc_typeFound" />
<exclude name="PHPCompatibility.Constants.NewConstants.pgsql_errors_sqlstateFound" />
<exclude name="PHPCompatibility.Constants.NewConstants.pgsql_pipeline_abortedFound" />
<exclude name="PHPCompatibility.Constants.NewConstants.pgsql_pipeline_offFound" />
<exclude name="PHPCompatibility.Constants.NewConstants.pgsql_pipeline_onFound" />
<exclude name="PHPCompatibility.Constants.NewConstants.pgsql_pipeline_syncFound" />
<exclude name="PHPCompatibility.Constants.NewConstants.pgsql_show_context_alwaysFound" />
<exclude name="PHPCompatibility.Constants.NewConstants.pgsql_show_context_errorsFound" />
<exclude name="PHPCompatibility.Constants.NewConstants.pgsql_show_context_neverFound" />
Expand Down Expand Up @@ -78,10 +75,6 @@
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.ldap_connect_walletFound" />
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.ldap_exop_syncFound" />
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.mb_str_padFound" />
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.pg_enter_pipeline_modeFound" />
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.pg_exit_pipeline_modeFound" />
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.pg_pipeline_statusFound" />
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.pg_pipeline_syncFound" />
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.pg_set_error_context_visibilityFound" />
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.posix_eaccessFound" />
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.posix_fpathconfFound" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<rule ref="PHPCompatibility.Constants.NewConstants.sodium_library_major_versionFound" />
<rule ref="PHPCompatibility.Constants.NewConstants.sodium_library_minor_versionFound" />
<rule ref="PHPCompatibility.Constants.NewConstants.sodium_library_versionFound" />
<rule ref="PHPCompatibility.FunctionUse.NewFunctions.array_is_listFound" />
<rule ref="PHPCompatibility.FunctionUse.NewFunctions.array_key_firstFound" />
<rule ref="PHPCompatibility.FunctionUse.NewFunctions.array_key_lastFound" />
<rule ref="PHPCompatibility.FunctionUse.NewFunctions.is_countableFound" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Update Jetpack-Compat rulesets.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Explicitly pass `$escape` for `fputcsv` to fix new phpcompatibility-dev sniff. Note we may want to change it once we drop PHP <7.4 compat.


Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,8 @@ public function download_feedback_as_csv() {
/**
* Print CSV headers
*/
fputcsv( $output, $fields );
// @todo When we drop support for PHP <7.4, consider passing empty-string for `$escape` here for better spec compatibility.
fputcsv( $output, $fields, ',', '"', '\\' );

/**
* Print rows to the output.
Expand All @@ -1820,7 +1821,8 @@ public function download_feedback_as_csv() {
/**
* Output the complete CSV row
*/
fputcsv( $output, $current_row );
// @todo When we drop support for PHP <7.4, consider passing empty-string for `$escape` here for better spec compatibility.
fputcsv( $output, $current_row, ',', '"', '\\' );
}

fclose( $output ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Improve spec compliance of CSV output: `\"` sequences will now be correctly escaped by doubling the `"`.
3 changes: 2 additions & 1 deletion projects/plugins/crm/includes/ZeroBSCRM.CSVImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ function jpcrm_csvimporter_lite_preflight_checks( $stage ) {

$file = fopen( $file_path, 'r' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
while ( ! feof( $file ) ) {
$csv_data[] = fgetcsv( $file );
// @todo Consider passing empty-string for `$escape` for better spec compatibility.
$csv_data[] = fgetcsv( $file, null, ',', '"', '\\' );

Check failure on line 257 in projects/plugins/crm/includes/ZeroBSCRM.CSVImporter.php

View workflow job for this annotation

GitHub Actions / Static analysis

TypeError PhanTypeMismatchArgumentInternalProbablyReal Argument 2 ($length) is null of type null but \fgetcsv() takes int
}

fclose( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
Expand Down
6 changes: 4 additions & 2 deletions projects/plugins/crm/includes/ZeroBSCRM.DAL3.Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ function jpcrm_export_process_file_export() {
$columnHeaders[] = __( 'Owner Username', 'zero-bs-crm' );
}
}
fputcsv( $output, $columnHeaders );
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- Just ignore until someone fixes all of these, otherwise we wind up having to rewrite half the function.
fputcsv( $output, $columnHeaders, ',', '"', '' );

// actual export lines

Expand Down Expand Up @@ -421,7 +422,8 @@ function jpcrm_export_process_file_export() {
} // / foreach field in each obj row

// output row
fputcsv( $output, $objRow );
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- Just ignore until someone fixes all of these, otherwise we wind up having to rewrite half the function.
fputcsv( $output, $objRow, ',', '"', '' );

} // / foreach obj
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Explicitly pass `$escape` for `str_getcsv` to fix new phpcompatibility-dev sniff. Note we may want to change it once we drop PHP <7.4 compat.


Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Replace broken `str_getcsv( $csv, "\n" )` with equally-broken `explode( "\n", $csv )` that at least doesn't pretend like it's not broken. Add a comment noting this too.


11 changes: 9 additions & 2 deletions projects/plugins/jetpack/modules/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -1667,8 +1667,15 @@ function stats_get_remote_csv( $url ) {
* @return array
*/
function stats_str_getcsv( $csv ) {
$lines = str_getcsv( $csv, "\n" );
return array_map( 'str_getcsv', $lines );
// @todo Correctly handle embedded newlines. Note, despite claims online, `str_getcsv( $csv, "\n" )` does not actually work.
$lines = explode( "\n", $csv );
return array_map(
function ( $line ) {
// @todo When we drop support for PHP <7.4, consider passing empty-string for `$escape` here for better spec compatibility.
return str_getcsv( $line, ',', '"', '\\' );
},
$lines
);
}

/**
Expand Down

0 comments on commit a9280a2

Please sign in to comment.