Skip to content

Commit 81b462c

Browse files
authored
AVRO-4194: remove PHP EOL versions and use new language features (#3528)
* remove PHP EOL version, add PHP cs fixer and fix test directory * add license to .php-cs-fixer.dist.php * fix missing quote in test-lang-php.yml * apply automatic changes to lib files and update AvroIO * update composer version in Dockerfile * add phpstan, initial cleanup of lib directory * Remove invalid covers annotations * fix build.sh * phpstan.neon license * improve AvroProtocol code & tests, add/fix types * fix AvroProtocol due to missing json_validate in php < 8.3 * add other types * add types and checks * remove squizlabs/php_codesniffer * fix some bugs and update static analysis to level 3 * add missing license * review - fixes * review - apply lint on lib * review - apply lint on lib * add doc field for fixed schemas
1 parent 64ac2cd commit 81b462c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+3246
-2841
lines changed

.github/workflows/test-lang-php.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ jobs:
4242
- ubuntu-latest
4343
- ubuntu-24.04-arm
4444
php:
45-
- '7.3'
46-
- '7.4'
47-
- '8.0'
4845
- '8.1'
4946
- '8.2'
5047
- '8.3'
@@ -71,8 +68,12 @@ jobs:
7168
${{ runner.os }}-composer-
7269
7370
- name: Lint
71+
if: matrix.php == '8.1'
7472
run: ./build.sh lint
7573

74+
- name: Static analysis
75+
run: ./build.sh phpstan
76+
7677
- name: Test
7778
run: ./build.sh test
7879

@@ -84,9 +85,6 @@ jobs:
8485
- ubuntu-latest
8586
- ubuntu-24.04-arm
8687
php:
87-
- '7.3'
88-
- '7.4'
89-
- '8.0'
9088
- '8.1'
9189
- '8.2'
9290
- '8.3'

composer.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,21 @@
2323
"issues": "https://issues.apache.org/jira/browse/AVRO"
2424
},
2525
"require": {
26-
"php": "^7.3 || ^8.0"
26+
"php": "^8.1"
2727
},
2828
"deps": [
29-
"vendor/phpunit/phpunit",
30-
"vendor/squizlabs/php_codesniffer"
29+
"vendor/phpunit/phpunit"
3130
],
3231
"require-dev": {
33-
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.5",
34-
"squizlabs/php_codesniffer": "3.*",
32+
"phpunit/phpunit": "^10.5",
3533
"php-mock/php-mock-phpunit": "^2.10",
3634
"ext-json": "*",
3735
"ext-xml": "*",
3836
"ext-curl": "*",
39-
"ext-pcntl": "*"
37+
"ext-pcntl": "*",
38+
"rector/rector": "^2.2",
39+
"friendsofphp/php-cs-fixer": "^3.89",
40+
"phpstan/phpstan": "^2.1"
4041
},
4142
"autoload": {
4243
"psr-4": {

lang/php/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
pkg
22
vendor
33
.phpunit.result.cache
4+
.phpunit.cache
5+
.php-cs-fixer.cache

lang/php/.php-cs-fixer.dist.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
/**
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* https://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
24+
25+
/**
26+
* PHP CS Fixer documentation:
27+
* - Homepage: https://cs.symfony.com/
28+
* - List of all available rules: https://cs.symfony.com/doc/rules/index.html
29+
* - List of all available rule sets: https://cs.symfony.com/doc/ruleSets/index.html
30+
*
31+
* To inspect a specific rule (e.g. `blank_line_before_statement`), run:
32+
*
33+
* ```console
34+
* > php-cs-fixer describe blank_line_before_statement
35+
* ```
36+
*/
37+
$finder = PhpCsFixer\Finder::create()
38+
->in(['lib', 'test'])
39+
->append(['.php-cs-fixer.dist.php']);
40+
41+
return (new PhpCsFixer\Config())
42+
->setRiskyAllowed(true)
43+
->setParallelConfig(ParallelConfigFactory::detect())
44+
->setRules([
45+
// —— CS Rule Sets —————————————————————————————————————————————————————
46+
'@Symfony' => true,
47+
'@PHP8x0Migration' => true,
48+
49+
// —— Overriden rules ——————————————————————————————————————————————————
50+
51+
// @Symfony: `['statements' => ['return']]`
52+
// Here using: default, `['statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try']]`
53+
'blank_line_before_statement' => true,
54+
55+
// @Symfony: `['tokens' => [['attribute', 'case', 'continue', 'curly_brace_block', 'default', 'extra', 'parenthesis_brace_block', 'square_brace_block', 'switch', 'throw', 'use']]]`
56+
// Here using: default, `['tokens' => ['extra']]`
57+
'no_extra_blank_lines' => true,
58+
59+
// @Symfony: `['allow_hidden_params' => true, 'remove_inheritdoc' => true]`
60+
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true, 'allow_unused_params' => true, 'remove_inheritdoc' => false],
61+
62+
// @Symfony: `['after_heredoc' => true]`
63+
// Here using: default, `['after_heredoc' => false]`
64+
'no_whitespace_before_comma_in_array' => true,
65+
66+
// @Symfony: `['order' => ['use_trait']]`
67+
// Here using: default, `['order' => ['use_trait', 'case', 'constant_public', 'constant_protected', 'constant_private', 'property_public', 'property_protected', 'property_private', 'construct', 'destruct', 'magic', 'phpunit', 'method_public', 'method_protected', 'method_private']]`)
68+
'ordered_class_elements' => true,
69+
70+
// @Symfony: `['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha']`
71+
// Here using: default, all import types (classes, functions, and constants) are sorted together alphabetically without grouping them by type
72+
'ordered_imports' => true,
73+
74+
// @Symfony: `['order' => ['param', 'return', 'throws']]`
75+
// Here using: default, `['param', 'throws', 'return']`
76+
'phpdoc_order' => true,
77+
78+
// @Symfony: `['null_adjustment' => 'always_last', 'sort_algorithm' => 'none']`
79+
'phpdoc_types_order' => ['null_adjustment' => 'always_first'],
80+
81+
// @Symfony: `['case' => 'camel_case']`
82+
'php_unit_method_casing' => ['case' => 'snake_case'],
83+
84+
// —— Overriden rules (disabled) ————————————————————————————————————————
85+
// @Symfony
86+
'increment_style' => false,
87+
'phpdoc_align' => false,
88+
'phpdoc_separation' => false,
89+
'phpdoc_summary' => false,
90+
'phpdoc_to_comment' => false,
91+
'single_line_comment_style' => false,
92+
'single_line_throw' => false,
93+
'single_quote' => false,
94+
95+
// —— Additional rules ——————————————————————————————————————————————————
96+
'no_useless_else' => true,
97+
'phpdoc_add_missing_param_annotation' => true,
98+
'phpdoc_annotation_without_dot' => false,
99+
'protected_to_private' => true,
100+
'psr_autoloading' => true,
101+
])
102+
->setFinder($finder)
103+
;

lang/php/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ A library for using [Avro](https://avro.apache.org/) with PHP.
88

99
Requirements
1010
============
11-
* PHP 7.3+
11+
* PHP 8.1+
1212
* On 32-bit platforms, the [GMP PHP extension](https://php.net/gmp)
1313
* For Zstandard compression, [ext-zstd](https://github.com/kjdev/php-ext-zstd)
1414
* For Snappy compression, [ext-snappy](https://github.com/kjdev/php-ext-snappy)
1515
* For testing, [PHPUnit](https://www.phpunit.de/)
1616

1717
Both GMP and PHPUnit are often available via package management
18-
systems as `php7-gmp` and `phpunit`, respectively.
18+
systems as `php8.1-gmp` and `phpunit`, respectively.
1919

2020

2121
Getting started

lang/php/build.sh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,22 @@ do
5959

6060
lint)
6161
composer install -d "../.."
62-
find . -name "*.php" -print0 | xargs -0 -n1 -P8 php -l
63-
../../vendor/bin/phpcs --standard=PSR12 lib
62+
../../vendor/bin/php-cs-fixer fix --dry-run --show-progress=dots --verbose --diff --no-interaction
63+
;;
64+
65+
lint-fix)
66+
composer install -d "../.."
67+
../../vendor/bin/php-cs-fixer fix --show-progress=dots --verbose --diff
68+
;;
69+
70+
phpstan)
71+
composer install -d "../.."
72+
../../vendor/bin/phpstan
6473
;;
6574

6675
test)
6776
composer install -d "../.."
68-
../../vendor/bin/phpunit -v
77+
../../vendor/bin/phpunit
6978
;;
7079

7180
dist)
@@ -77,7 +86,7 @@ do
7786
;;
7887

7988
*)
80-
echo "Usage: $0 {interop-data-generate|test-interop|lint|test|dist|clean}"
89+
echo "Usage: $0 {interop-data-generate|test-interop|lint|lint-fix|test|dist|clean}"
8190
esac
8291
done
8392

lang/php/examples/write_read.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
{"name":"member_name", "type":"string"}]}
3838
_JSON;
3939

40-
$jose = array('member_id' => 1392, 'member_name' => 'Jose');
41-
$maria = array('member_id' => 1642, 'member_name' => 'Maria');
42-
$data = array($jose, $maria);
40+
$jose = ['member_id' => 1392, 'member_name' => 'Jose'];
41+
$maria = ['member_id' => 1642, 'member_name' => 'Maria'];
42+
$data = [$jose, $maria];
4343

4444
$file_name = 'data.avr';
4545
// Open $file_name for writing, using the given writer's schema

lang/php/lib/Avro.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
*
2828
* This port is an implementation of the
2929
* {@link https://avro.apache.org/docs/1.3.3/spec.html Avro 1.3.3 Specification}
30-
*
31-
* @package Avro
3230
*/
3331
class Avro
3432
{
@@ -46,49 +44,47 @@ class Avro
4644
private const GMP_BIGINTEGER_MODE = 0x01;
4745
/**#@-*/
4846
/**
49-
* @var int
5047
* Mode used to handle bigintegers. After Avro::check64Bit() has been called,
5148
* (usually via a call to Avro::checkPlatform(), set to
5249
* self::GMP_BIGINTEGER_MODE on 32-bit platforms that have GMP available,
5350
* and to self::PHP_BIGINTEGER_MODE otherwise.
5451
*/
55-
private static $biginteger_mode;
52+
private static int $biginteger_mode;
5653

5754
/**
5855
* Wrapper method to call each required check.
59-
*
6056
*/
6157
public static function checkPlatform()
6258
{
6359
self::check64Bit();
6460
}
6561

62+
/**
63+
* @returns bool true if the PHP GMP extension is used and false otherwise.
64+
* @internal Requires Avro::check64Bit() (exposed via Avro::checkPlatform())
65+
* to have been called to set Avro::$biginteger_mode.
66+
*/
67+
public static function usesGmp(): bool
68+
{
69+
return self::GMP_BIGINTEGER_MODE === self::$biginteger_mode;
70+
}
71+
6672
/**
6773
* Determines if the host platform can encode and decode long integer data.
6874
*
6975
* @throws AvroException if the platform cannot handle long integers.
7076
*/
7177
private static function check64Bit()
7278
{
73-
if (8 != PHP_INT_SIZE) {
79+
if (8 !== PHP_INT_SIZE) {
7480
if (extension_loaded('gmp')) {
7581
self::$biginteger_mode = self::GMP_BIGINTEGER_MODE;
7682
} else {
7783
throw new AvroException('This platform cannot handle a 64-bit operations. '
78-
. 'Please install the GMP PHP extension.');
84+
.'Please install the GMP PHP extension.');
7985
}
8086
} else {
8187
self::$biginteger_mode = self::PHP_BIGINTEGER_MODE;
8288
}
8389
}
84-
85-
/**
86-
* @returns boolean true if the PHP GMP extension is used and false otherwise.
87-
* @internal Requires Avro::check64Bit() (exposed via Avro::checkPlatform())
88-
* to have been called to set Avro::$biginteger_mode.
89-
*/
90-
public static function usesGmp()
91-
{
92-
return self::GMP_BIGINTEGER_MODE === self::$biginteger_mode;
93-
}
9490
}

0 commit comments

Comments
 (0)