From 78ff78887728af53011dce49ebc4e9b7cf2ac816 Mon Sep 17 00:00:00 2001 From: Dick van der Heiden Date: Sat, 24 May 2025 11:41:23 +0200 Subject: [PATCH 1/2] Modernize package --- .editorconfig | 15 ------- .github/ISSUE_TEMPLATE/bug_report.md | 5 +-- .github/ISSUE_TEMPLATE/feature_request.md | 7 ++-- .github/PULL_REQUEST_TEMPLATE.md | 5 --- .github/workflows/ci.yml | 45 +++++++++++---------- .gitignore | 9 +++-- README.md | 48 ++++++++++------------- composer.json | 23 ++++++----- phpstan.neon.dist | 6 +++ phpunit.xml | 23 +++++++++++ phpunit.xml.dist | 16 -------- pint.json | 3 ++ psalm.xml | 15 ------- src/Builder.php | 45 +++++++++++++-------- src/Parameter.php | 31 +++++---------- tests/ParameterTest.php | 44 --------------------- tests/{ => Unit}/BuilderTest.php | 19 +++++---- tests/Unit/ParameterTest.php | 44 +++++++++++++++++++++ 18 files changed, 193 insertions(+), 210 deletions(-) delete mode 100644 .editorconfig create mode 100644 phpstan.neon.dist create mode 100644 phpunit.xml delete mode 100644 phpunit.xml.dist create mode 100644 pint.json delete mode 100644 psalm.xml delete mode 100644 tests/ParameterTest.php rename tests/{ => Unit}/BuilderTest.php (61%) create mode 100644 tests/Unit/ParameterTest.php diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index cd8eb86..0000000 --- a/.editorconfig +++ /dev/null @@ -1,15 +0,0 @@ -; This file is for unifying the coding style for different editors and IDEs. -; More information at http://editorconfig.org - -root = true - -[*] -charset = utf-8 -indent_size = 4 -indent_style = space -end_of_line = lf -insert_final_newline = true -trim_trailing_whitespace = true - -[*.md] -trim_trailing_whitespace = false diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index baefe38..5f1b338 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -1,10 +1,9 @@ --- name: Bug report -about: Create a report to help us improve +about: Create a report to help us improve the package title: "" labels: bug -assignees: vdhicts - +assignees: dvdheiden --- ## Describe the bug diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index afed919..03d7347 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -1,15 +1,14 @@ --- name: Feature request -about: Suggest an idea for this project +about: Suggest an idea for this package title: "" labels: feature -assignees: vdhicts - +assignees: dvdheiden --- ## Goal -A clear and concise description of what the problem is. Ex. I think this could be easier when... +A clear and concise description of which "problem" you want to get solved. Ex. I think this could be easier when... ## Additional information diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index d611a80..e7e36af 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,8 +1,3 @@ # Changes Provide a summary of your changes. - -# Checks - -- [ ] The changelog is updated (when applicable) -- [ ] The support Cluster API version is updated in the readme (when applicable) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bf0a63a..c5ad588 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,29 +1,34 @@ -name: http-query-builder +name: CI -on: [push] +on: [ push ] jobs: - cluster-api-tests: - runs-on: ubuntu-latest + tests: + runs-on: ubuntu-latest + strategy: + matrix: + php-versions: [ '8.3', '8.4' ] - steps: - - name: Checkout - uses: actions/checkout@v1 + name: PHP ${{ matrix.php-versions }} - - name: Setup PHP - uses: shivammathur/setup-php@v1 - with: - php-version: '7.4' - extensions: mbstring, intl + steps: + - name: Checkout + uses: actions/checkout@v4 - - name: Install Dependencies - run: composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + extensions: mbstring, intl - - name: Execute tests (Unit and Feature tests) via PHPUnit - run: | - vendor/bin/phpunit + - name: Install composer dependencies + run: composer install --no-ansi --no-interaction --no-scripts --no-suggest --no-progress - - name: Execute static analysis - run: | - vendor/bin/psalm + - name: Execute static analysis + run: composer analyse + - name: Execute tests via PHPUnit + run: composer test:no-coverage + + - name: Check code style + run: composer code-style:check diff --git a/.gitignore b/.gitignore index a5bc3c6..b71aab7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ +/vendor composer.lock -/vendor/ -/.idea/ -/build/ +/build .phpunit.result.cache +.idea +.phpunit.cache +.DS_Store +.phpactor.json diff --git a/README.md b/README.md index 940e8c5..953783d 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,21 @@ # http-query-builder -Replacement of http_build_query to allow the same parameter multiple times. +Replacement of `http_build_query` to allow the same parameter multiple times. ## Requirements -This package requires PHP 7.4 or higher. +This package requires PHP 8.3 or higher. ## Installation -This package can be used in any PHP project or with any framework. - You can install the package via composer: `composer require vdhicts/http-query-builder` ## Usage -The problem with the build-in `http_build_query` method is that it doesn't accept the same parameter multiple times. -When you need to consume an API that uses those parameters (for example [FastAPI](https://fastapi.tiangolo.com/) +The problem with the build-in `http_build_query` method is that it doesn't accept the same parameter multiple times as +it overwrites the key in the array. When you need to consume an API that uses those parameters (for example [FastAPI](https://fastapi.tiangolo.com/) supports it), this package comes in handy. ### Getting started @@ -25,37 +23,31 @@ supports it), this package comes in handy. ```php use Vdhicts\HttpQueryBuilder\Builder; -$builder = (new Builder()) +$builder = Builder::make() ->add('filter', 'a:1') ->add('filter', 'b:2'); echo $builder; // filter=a%3A1&filter=b%3A2 ``` -## Tests - -Unit tests are available in the `tests` folder. Run with: - -`composer test` - -When you want a code coverage report which will be generated in the `build/report` folder. Run with: +## Contributing -`composer test-coverage` +Found a bug or want to add a new feature? Great! There are also many other ways to make meaningful contributions such +as reviewing outstanding pull requests and writing documentation. Even opening an issue for a bug you found is +appreciated. -## Contribution +When you create a pull request, make sure it is tested, following the code standard (run `composer code-style:fix` to +take care of that for you) and please create one pull request per feature. In exchange, you will be credited as +contributor. -Any contribution is welcome, but it should meet the PSR-2 standard and please create one pull request per feature/bug. -In exchange, you will be credited as contributor on this page. +### Testing -## Security +To run the tests, you can use the following command: -If you discover any security related issues in this or other packages of Vdhicts, please email info@vdhicts.nl instead -of using the issue tracker. - -## License - -This package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT). +```bash +composer test +``` -## About Vdhicts +### Security -[Vdhicts](https://www.vdhicts.nl) is the name of my personal company for which I work as freelancer. Vdhicts develops -and implements IT solutions for businesses and educational institutions. +If you discover any security related issues in this or other packages of Vdhicts!, please email security@vdhicts.nl +instead of using the issue tracker. diff --git a/composer.json b/composer.json index 40a90da..2895084 100644 --- a/composer.json +++ b/composer.json @@ -1,26 +1,27 @@ { "name": "vdhicts/http-query-builder", - "description": "Replacement of http_build_query to allow the same parameter multiple times ", + "description": "Replacement of http_build_query to allow the same parameter multiple times.", "keywords": [ "http_build_query", - "query" + "query", + "builder" ], "homepage": "https://github.com/vdhicts/http-query-builder", "license": "MIT", "authors": [ { "name": "Dick van der Heiden", - "email": "info@vdhicts.nl", - "homepage": "https://www.vdhicts.nl", + "email": "dick@goedemiddag.nl", "role": "Developer" } ], "require": { - "php": "^7.4 || ^8.0" + "php": "^8.3" }, "require-dev": { - "phpunit/phpunit": "^9.0", - "vimeo/psalm": "^4.4" + "laravel/pint": "^1.22", + "phpstan/phpstan": "^2.1", + "phpunit/phpunit": "^11.0" }, "autoload": { "psr-4": { @@ -33,9 +34,11 @@ } }, "scripts": { - "test": "vendor/bin/phpunit --no-coverage", - "test-coverage": "vendor/bin/phpunit", - "analyse": "vendor/bin/psalm" + "analyse": "vendor/bin/phpstan analyse", + "code-style:check": "vendor/bin/pint --test", + "code-style:fix": "vendor/bin/pint", + "test": "vendor/bin/phpunit", + "test:no-coverage": "vendor/bin/phpunit --no-coverage" }, "config": { "sort-packages": true diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..5d7a538 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,6 @@ +parameters: + paths: + - src/ + - tests/ + level: 6 + treatPhpDocTypesAsCertain: false \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..bf06695 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,23 @@ + + + + + + + + + + tests + + + + + + src + + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100644 index 8d0ec34..0000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,16 +0,0 @@ - - - - - src/ - - - - - - - - tests - - - \ No newline at end of file diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..ef654c6 --- /dev/null +++ b/pint.json @@ -0,0 +1,3 @@ +{ + "preset": "psr12" +} \ No newline at end of file diff --git a/psalm.xml b/psalm.xml deleted file mode 100644 index da5d2e5..0000000 --- a/psalm.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/src/Builder.php b/src/Builder.php index 03bed1e..b9a217e 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -2,36 +2,49 @@ namespace Vdhicts\HttpQueryBuilder; -class Builder +use Stringable; + +class Builder implements Stringable { - private array $parameters = []; + /** + * @param array $parameters + */ + public function __construct( + public array $parameters = [], + ) { + } - public function get(): array + /** + * @param array $parameters + */ + public static function make(array $parameters = []): self { - return $this->parameters; + return new self($parameters); } - public function add(string $parameter, string $value): Builder + public function add(string $parameter, int|string $value): Builder { - $this->parameters[] = new Parameter($parameter, $value); + $this->parameters[] = new Parameter( + key: $parameter, + value: $value, + ); return $this; } - public function build(): string + public function toString(): string { - $queryParameters = array_map( - function (Parameter $parameter) { - return $parameter->build(); - }, - $this->parameters + return implode( + separator: '&', + array: array_map( + static fn (Parameter $parameter) => $parameter->toString(), + $this->parameters, + ), ); - - return implode('&', $queryParameters); } - public function __toString() + public function __toString(): string { - return $this->build(); + return $this->toString(); } } diff --git a/src/Parameter.php b/src/Parameter.php index f81e565..8ae673d 100644 --- a/src/Parameter.php +++ b/src/Parameter.php @@ -2,38 +2,27 @@ namespace Vdhicts\HttpQueryBuilder; -class Parameter -{ - private string $key; - private string $value; +use Stringable; - public function __construct(string $key, string $value) - { - $this->key = $key; - $this->value = $value; - } - - public function getKey(): string - { - return $this->key; - } - - public function getValue(): string - { - return $this->value; +class Parameter implements Stringable +{ + public function __construct( + public string $key, + public int|string $value + ) { } - public function build(): string + public function toString(): string { return sprintf( '%s=%s', urlencode($this->key), - urlencode($this->value) + urlencode((string) $this->value) ); } public function __toString(): string { - return $this->build(); + return $this->toString(); } } diff --git a/tests/ParameterTest.php b/tests/ParameterTest.php deleted file mode 100644 index f35ae26..0000000 --- a/tests/ParameterTest.php +++ /dev/null @@ -1,44 +0,0 @@ -assertSame($key, $parameter->getKey()); - $this->assertSame($value, $parameter->getValue()); - - // Integers - $key = 1; - $value = 2; - $parameter = new Parameter($key, $value); - $this->assertSame((string)$key, $parameter->getKey()); - $this->assertSame((string)$value, $parameter->getValue()); - } - - public function testParameterBuild() - { - $parameter = new Parameter('string', 'string'); - $this->assertSame('string=string', $parameter->build()); - - $parameter = new Parameter(1, 2); - $this->assertSame('1=2', $parameter->build()); - - $parameter = new Parameter('string', 'string:string'); - $this->assertSame('string=string%3Astring', $parameter->build()); - } - - public function testSerialization() - { - $parameter = new Parameter('string', 'string'); - $this->assertSame('string=string', (string)$parameter); - } -} diff --git a/tests/BuilderTest.php b/tests/Unit/BuilderTest.php similarity index 61% rename from tests/BuilderTest.php rename to tests/Unit/BuilderTest.php index a85074a..053aded 100644 --- a/tests/BuilderTest.php +++ b/tests/Unit/BuilderTest.php @@ -1,35 +1,34 @@ add('filter', 'a:1') ->add('filter', 'b:2') ->add('test', 1); } - public function testBuilder() + public function test_builder(): void { $builder = $this->initBuilder(); - $this->assertIsArray($builder->get()); - $this->assertCount(3, $builder->get()); - $this->assertInstanceOf(Parameter::class, $builder->get()[0]); - $this->assertSame('filter=a%3A1&filter=b%3A2&test=1', $builder->build()); + $this->assertCount(3, $builder->parameters); + $this->assertInstanceOf(Parameter::class, $builder->parameters[0]); + $this->assertSame('filter=a%3A1&filter=b%3A2&test=1', $builder->toString()); } - public function testSerialization() + public function test_serialization(): void { $builder = $this->initBuilder(); - $this->assertSame('filter=a%3A1&filter=b%3A2&test=1', (string)$builder); + $this->assertSame('filter=a%3A1&filter=b%3A2&test=1', (string) $builder); } } diff --git a/tests/Unit/ParameterTest.php b/tests/Unit/ParameterTest.php new file mode 100644 index 0000000..5358213 --- /dev/null +++ b/tests/Unit/ParameterTest.php @@ -0,0 +1,44 @@ +assertSame($key, $parameter->key); + $this->assertSame($value, $parameter->value); + + // Integers + $key = 'test'; + $value = 2; + $parameter = new Parameter($key, $value); + $this->assertSame($key, $parameter->key); + $this->assertSame($value, $parameter->value); + } + + public function test_parameter_build(): void + { + $parameter = new Parameter('string', 'string'); + $this->assertSame('string=string', $parameter->toString()); + + $parameter = new Parameter('test', 2); + $this->assertSame('test=2', $parameter->toString()); + + $parameter = new Parameter('string', 'string:string'); + $this->assertSame('string=string%3Astring', $parameter->toString()); + } + + public function test_serialization(): void + { + $parameter = new Parameter('string', 'string'); + $this->assertSame('string=string', (string) $parameter); + } +} From cbcba0c7c71855ac272cc7200a48cbb3fd1690be Mon Sep 17 00:00:00 2001 From: Dick van der Heiden Date: Sat, 24 May 2025 11:45:31 +0200 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b6d290..10b3356 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,26 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) cluster API. See the changelog of the [cluster API](https://cluster-api.cyberfusion.nl/redoc#section/Changelog) for detailed information. +## [2.0.0] + +### Added + +- Add support for PHP 8.3+ and modernize code. +- Add test for parameters. +- Add PHPStan for static analysis. +- Add Pint for code style checks/enforcement. +- Add implementation of the `Stringable` interface. + +### Changed + +- Updated dependencies to their latest versions. + +### Removed + +- Remove psalm in favor of Pint. + ## [1.0.0] -### [Added] +### Added - Add initial version of the package