From 8fc2887861425241494af210cc29bb2b37a27c47 Mon Sep 17 00:00:00 2001 From: Damien Debin Date: Wed, 15 Jun 2022 16:54:40 +0200 Subject: [PATCH] Adds/test compatibility with PHP 8.1. --- .github/workflows/main.yml | 2 +- composer.json | 2 +- lib/MC/Google/Visualization.php | 26 +++++++++++++------------- lib/MC/Parser/Def/NOrMore.php | 2 +- lib/MC/Parser/Def/OneOf.php | 2 +- phpstan.neon | 1 + tests/ExampleTest.php | 8 ++++---- tests/result3.js | 2 +- 8 files changed, 23 insertions(+), 22 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e50a6e8..4dababf 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: ['7.1', '7.2', '7.3', '7.4', '8.0'] + php: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1'] name: PHP ${{ matrix.php }} tests steps: - uses: actions/checkout@v2 diff --git a/composer.json b/composer.json index 38717bf..559f7a6 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ } ], "require": { - "php": "^7.1 || 8.0.*", + "php": "^7.1 || ^8.0", "ext-json": "*", "ext-pdo": "*" }, diff --git a/lib/MC/Google/Visualization.php b/lib/MC/Google/Visualization.php index 820508f..7ca010d 100644 --- a/lib/MC/Google/Visualization.php +++ b/lib/MC/Google/Visualization.php @@ -214,7 +214,7 @@ public function handleQuery(string $query, array $params): string $stmt = $this->db->query($sql); assert(false !== $stmt); - //If we got here, there's no errors + // If we got here, there's no errors $response .= $this->getSuccessInit($meta); $first = true; foreach ($stmt as $row) { @@ -272,11 +272,11 @@ public function generateMetadata(array $query): array } if (!isset($query['select'])) { - //By default, return all fields defined for an entity + // By default, return all fields defined for an entity $query['select'] = array_keys($entity['fields']); } - //The query fields might be different from the "select" fields (callback dependant fields will not be returned) + // The query fields might be different from the "select" fields (callback dependant fields will not be returned) $meta['query_fields'] = []; $meta['joins'] = []; $meta['field_spec'] = []; @@ -313,7 +313,7 @@ public function generateMetadata(array $query): array $meta['select'] = $query['select']; if (isset($query['where'])) { - //Parse the where clauses and error out on non-existant and callback fields and add joins + // Parse the where clauses and error out on non-existant and callback fields and add joins foreach ($query['where'] as $whereToken) { if ('where_field' === $whereToken['type']) { $field = $whereToken['value']; @@ -334,7 +334,7 @@ public function generateMetadata(array $query): array } } - //Also add the joins & field spec information for the orderby, groupby, and pivot clauses + // Also add the joins & field spec information for the orderby, groupby, and pivot clauses if (isset($query['pivot'])) { foreach ($query['pivot'] as $field) { if (!isset($entity['fields'][$field])) { @@ -393,7 +393,7 @@ public function generateMetadata(array $query): array } } - //Some of the query information we just copy into the metadata array + // Some of the query information we just copy into the metadata array $copyKeys = ['where', 'orderby', 'groupby', 'pivot', 'limit', 'offset', 'labels', 'formats', 'options']; foreach ($copyKeys as $copyKey) { if (isset($query[$copyKey])) { @@ -544,7 +544,7 @@ public function getRowValues(array $row, array $meta): string $time = strtotime($year.'0104 +'.$week.' weeks'); assert(false !== $time); $monday = strtotime('-'.((int) date('w', $time) - 1).' days', $time); - assert(false !== $monday); + assert(false !== $monday); // @phpstan-ignore-line ; PHP < 8.0 [$year, $month, $day] = explode('-', date('Y-m-d', $monday)); $formatted = date($format, $monday); } else { @@ -927,7 +927,7 @@ protected function generateSQL(array &$meta): string } if (isset($meta['pivot'])) { - //Pivot queries are special - they require an entity to be passed and modify the query directly + // Pivot queries are special - they require an entity to be passed and modify the query directly $entity = $meta['entity']; $pivotFields = []; $pivotJoins = []; @@ -965,9 +965,9 @@ protected function generateSQL(array &$meta): string $stmt = $this->db->query($pivotSql); assert(false !== $stmt); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); - assert(is_array($rows)); + assert(false !== $rows); // @phpstan-ignore-line ; PHP < 8.0 foreach ($rows as $row) { - //Create a version of all function-ed fields for each unique combination of pivot values + // Create a version of all function-ed fields for each unique combination of pivot values foreach ($funcFields as $field) { $field[2] = $row; @@ -975,7 +975,7 @@ protected function generateSQL(array &$meta): string } } - //For pivot queries, the fields we return and the fields we query against are always the same + // For pivot queries, the fields we return and the fields we query against are always the same $meta['select'] = $meta['query_fields']; } @@ -996,7 +996,7 @@ protected function generateSQL(array &$meta): string if (isset($meta['where'])) { $where = []; foreach ($meta['where'] as &$wherePart) { - //Replace field references with their SQL forms + // Replace field references with their SQL forms switch ($wherePart['type']) { case 'where_field': $wherePart['value'] = $this->getFieldSQL($wherePart['value'], $meta['field_spec'][$wherePart['value']]); @@ -1058,7 +1058,7 @@ protected function generateSQL(array &$meta): string $first = true; foreach ($meta['orderby'] as $field => $dir) { if (isset($meta['field_spec'][$field]['sort_field'])) { - //An entity field can delegate sorting to another field by using the "sort_field" key + // An entity field can delegate sorting to another field by using the "sort_field" key $field = $meta['field_spec'][$field]['sort_field']; } $spec = $meta['field_spec'][$field]; diff --git a/lib/MC/Parser/Def/NOrMore.php b/lib/MC/Parser/Def/NOrMore.php index ab0681b..ddc1486 100644 --- a/lib/MC/Parser/Def/NOrMore.php +++ b/lib/MC/Parser/Def/NOrMore.php @@ -43,7 +43,7 @@ public function _parse(string $str, int $loc): array } if (0 === $toks->count()) { - //If this token is empty, remove it from the result group + // If this token is empty, remove it from the result group $toks = null; } diff --git a/lib/MC/Parser/Def/OneOf.php b/lib/MC/Parser/Def/OneOf.php index 20dff16..164bbc0 100644 --- a/lib/MC/Parser/Def/OneOf.php +++ b/lib/MC/Parser/Def/OneOf.php @@ -38,7 +38,7 @@ public function _parse(string $str, int $loc): array $res = $toks; } } catch (ParseError $parseError) { - //Ignore any non-matching conditions + // Ignore any non-matching conditions } } diff --git a/phpstan.neon b/phpstan.neon index 101e9e9..1d162c6 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,3 +4,4 @@ includes: - vendor/phpstan/phpstan-strict-rules/rules.neon parameters: checkMissingIterableValueType: false + reportUnmatchedIgnoredErrors: false \ No newline at end of file diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index bcf1257..1756644 100644 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -57,7 +57,7 @@ public function testQueryComplete(): void $output = $vis->handleRequest(false, $parameters); - //file_put_contents(__DIR__.'/result1.js', $output); + // file_put_contents(__DIR__.'/result1.js', $output); self::assertStringEqualsFile(__DIR__.'/result1.js', $output); } @@ -82,7 +82,7 @@ public function testQuerySimple(): void $output = $vis->handleRequest(false, $parameters); - //file_put_contents(__DIR__.'/result2.js', $output); + // file_put_contents(__DIR__.'/result2.js', $output); self::assertStringEqualsFile(__DIR__.'/result2.js', $output); } @@ -111,13 +111,13 @@ public function testQueryJoins(): void ]); $parameters = [ - 'tq' => 'select avg(life_male), avg(life_female), avg(life_both) from countries label life_male "Life Expectancy (Male)", life_female "Life Expectancy (Female)", life_both "Life Expectancy (Combined)" format life_male "%.2f years", life_female "%.2f years", life_both "%.2f years"', + 'tq' => 'select max(life_male), avg(life_female), min(life_both) from countries label life_male "Life Expectancy (Male)", life_female "Life Expectancy (Female)", life_both "Life Expectancy (Combined)" format life_male "%.2f years", life_female "%.2f years", life_both "%.2f years"', 'tqx' => 'reqId:3', ]; $output = $vis->handleRequest(false, $parameters); - //file_put_contents(__DIR__.'/result3.js', $output); + // file_put_contents(__DIR__.'/result3.js', $output); self::assertStringEqualsFile(__DIR__.'/result3.js', $output); } } diff --git a/tests/result3.js b/tests/result3.js index d7f9900..86c87b6 100644 --- a/tests/result3.js +++ b/tests/result3.js @@ -1 +1 @@ -google.visualization.Query.setResponse({version:'0.5',reqId:'3',status:'ok',table:{cols: [{id:'avg-life_male',label:"Life Expectancy (Male)",type:'number'},{id:'avg-life_female',label:"Life Expectancy (Female)",type:'number'},{id:'avg-life_both',label:"Life Expectancy (Combined)",type:'number'}],rows: [{c:[{v:64.5506078599412,f:"64.55 years"},{v:69.2214973065625,f:"69.22 years"},{v:66.761690744368,f:"66.76 years"}]}]}}); \ No newline at end of file +google.visualization.Query.setResponse({version:'0.5',reqId:'3',status:'ok',table:{cols: [{id:'max-life_male',label:"Life Expectancy (Male)",type:'number'},{id:'avg-life_female',label:"Life Expectancy (Female)",type:'number'},{id:'min-life_both',label:"Life Expectancy (Combined)",type:'number'}],rows: [{c:[{v:82.15,f:"82.15 years"},{v:69.2214973065625,f:"69.22 years"},{v:0,f:"0.00 years"}]}]}}); \ No newline at end of file