From c118272654d0bbf5e102126555f1091eafc1152a Mon Sep 17 00:00:00 2001 From: Mateusz Date: Tue, 2 Jun 2026 16:24:17 +0100 Subject: [PATCH] PCBC-1063: Testing Docs SDK code samples --- .github/workflows/check-examples.yml | 52 +++++++++ composer.json | 3 + composer.lock | 104 +++++++++++++++--- modules/howtos/examples/kv-crud.php | 4 + modules/howtos/examples/search.php | 3 + .../examples/subdoc-mutatein-arrays.php | 2 +- .../howtos/examples/transactions-example.php | 13 ++- modules/howtos/examples/using-cas.php | 6 +- phpstan.neon | 2 + 9 files changed, 168 insertions(+), 21 deletions(-) create mode 100644 .github/workflows/check-examples.yml create mode 100644 phpstan.neon diff --git a/.github/workflows/check-examples.yml b/.github/workflows/check-examples.yml new file mode 100644 index 00000000..e68709dc --- /dev/null +++ b/.github/workflows/check-examples.yml @@ -0,0 +1,52 @@ +name: Check Examples + +on: + pull_request: + paths: + - 'modules/**/examples/**' + - '.github/workflows/check-examples.yml' + workflow_dispatch: + inputs: + sdk_commit_sha: + description: 'Commit SHA of couchbase-php-client to use (leave blank for latest main)' + required: false + default: '' + +jobs: + check-examples: + name: Lint Examples + runs-on: ubuntu-latest + + steps: + - name: Checkout docs-sdk-php + uses: actions/checkout@v4 + + - name: Checkout couchbase-php-client SDK + uses: actions/checkout@v4 + with: + repository: couchbase/couchbase-php-client + path: couchbase-php-client + ref: ${{ (github.event_name == 'workflow_dispatch' && inputs.sdk_commit_sha != '') && inputs.sdk_commit_sha || 'main' }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.1' + tools: composer, phpstan + + - name: Patch composer.json to use local SDK + run: | + composer config repositories.couchbase-php-client '{"type": "path", "url": "./couchbase-php-client", "options": {"symlink": false}}' + composer require --no-update couchbase/couchbase:@dev + composer require --no-update --dev phpstan/phpstan + composer update --no-interaction --prefer-dist + + - name: Run PHPStan on examples + run: | + php -d memory_limit=512M vendor/bin/phpstan analyse \ + --configuration=phpstan.neon \ + --no-progress \ + $(find modules -name "*.php" -path "*/examples/*" | tr '\n' ' ') + + + diff --git a/composer.json b/composer.json index 04de376f..981c5e5a 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,8 @@ { "require": { "couchbase/couchbase": "^4.0" + }, + "require-dev": { + "phpstan/phpstan": "^2.2" } } diff --git a/composer.lock b/composer.lock index 7296c9e4..d07b053e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,20 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "fa3f142633f474874cf71c8e5c890557", + "content-hash": "905d5dc22d85ee317433e1896ffecd08", "packages": [ { "name": "couchbase/couchbase", - "version": "4.0.0", + "version": "4.5.0", "source": { "type": "git", - "url": "git@github.com:couchbase/couchbase-php-client.git", - "reference": "12ca305b72d36cc60ebffca0aa2c7e1cfc3ab803" + "url": "https://github.com/couchbase/couchbase-php-client.git", + "reference": "41d3b2bd484d8a823d7b236df9b7973021237f3d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/couchbase/couchbase-php-client/zipball/12ca305b72d36cc60ebffca0aa2c7e1cfc3ab803", - "reference": "12ca305b72d36cc60ebffca0aa2c7e1cfc3ab803", + "url": "https://api.github.com/repos/couchbase/couchbase-php-client/zipball/41d3b2bd484d8a823d7b236df9b7973021237f3d", + "reference": "41d3b2bd484d8a823d7b236df9b7973021237f3d", "shasum": "" }, "require": { @@ -27,10 +27,19 @@ "ext-sockets": "*", "phpunit/phpunit": "^9" }, + "suggest": { + "ext-grpc": "^1.15", + "ext-protobuf": "^3.21", + "google/common-protos": "^3.1", + "google/protobuf": "^3.21", + "grpc/grpc": "^1.42" + }, "type": "library", "autoload": { "psr-4": { - "Couchbase\\": "Couchbase/" + "Couchbase\\": "Couchbase/", + "GPBMetadata\\Couchbase\\": "GPBMetadata/Couchbase", + "Couchbase\\Protostellar\\": "Couchbase/Protostellar" } }, "notification-url": "https://packagist.org/downloads/", @@ -44,16 +53,85 @@ } ], "description": "The PHP client library provides fast access to documents stored in a Couchbase Server.", - "time": "2022-05-11T08:48:37+00:00" + "support": { + "issues": "https://github.com/couchbase/couchbase-php-client/issues", + "source": "https://github.com/couchbase/couchbase-php-client/tree/4.5.0" + }, + "time": "2026-04-01T23:20:49+00:00" + } + ], + "packages-dev": [ + { + "name": "phpstan/phpstan", + "version": "2.2.1", + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/dea9c8f2d25cc849391042b71e429c1a4bf82660", + "reference": "dea9c8f2d25cc849391042b71e429c1a4bf82660", + "shasum": "" + }, + "require": { + "php": "^7.4|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ondřej Mirtes" + }, + { + "name": "Markus Staab" + }, + { + "name": "Vincent Langlet" + } + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + } + ], + "time": "2026-05-28T14:44:12+00:00" } ], - "packages-dev": [], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": {}, "prefer-stable": false, "prefer-lowest": false, - "platform": [], - "platform-dev": [], - "plugin-api-version": "2.3.0" + "platform": {}, + "platform-dev": {}, + "plugin-api-version": "2.6.0" } diff --git a/modules/howtos/examples/kv-crud.php b/modules/howtos/examples/kv-crud.php index 4f630e3d..53ebf71c 100644 --- a/modules/howtos/examples/kv-crud.php +++ b/modules/howtos/examples/kv-crud.php @@ -8,6 +8,10 @@ use \Couchbase\GetOptions; use \Couchbase\RemoveOptions; use \Couchbase\DurabilityLevel; +use \Couchbase\RangeScan; +use \Couchbase\PrefixScan; +use \Couchbase\SamplingScan; +use \Couchbase\ScanOptions; $opts = new ClusterOptions(); $opts->credentials("Administrator", "password"); diff --git a/modules/howtos/examples/search.php b/modules/howtos/examples/search.php index a5962842..2c2e6e6c 100644 --- a/modules/howtos/examples/search.php +++ b/modules/howtos/examples/search.php @@ -7,6 +7,9 @@ use \Couchbase\ConjunctionSearchQuery; use \Couchbase\SearchOptions; use \Couchbase\MutationState; +use \Couchbase\SearchRequest; +use \Couchbase\VectorSearch; +use \Couchbase\VectorQuery; /* * index definition diff --git a/modules/howtos/examples/subdoc-mutatein-arrays.php b/modules/howtos/examples/subdoc-mutatein-arrays.php index c6ea9c05..a2beeada 100644 --- a/modules/howtos/examples/subdoc-mutatein-arrays.php +++ b/modules/howtos/examples/subdoc-mutatein-arrays.php @@ -23,7 +23,7 @@ // #tag::mutateInArrayPrepend[] $result = $collection->MutateIn("customer123", [ - new \Couchbase\MutateArrayPrependspec("purchases.abandoned", [18]) + new \Couchbase\MutateArrayPrependSpec("purchases.abandoned", [18]) ]); // purchases.abandoned is now [18, 157, 49, 999] // #end::mutateInArrayPrepend[] diff --git a/modules/howtos/examples/transactions-example.php b/modules/howtos/examples/transactions-example.php index 342cc677..e53a5704 100644 --- a/modules/howtos/examples/transactions-example.php +++ b/modules/howtos/examples/transactions-example.php @@ -243,6 +243,11 @@ function (TransactionAttemptContext $ctx) use ($collection) { // end::insert[] } +function calculateLevelForExperience(int $experience): int +{ + return (int)floor($experience / 100); +} + function queryExamples($cluster) { echo "\nRunning: queryExamplesSelect\n"; @@ -306,12 +311,11 @@ function (TransactionAttemptContext $ctx) use ($hotelChain, $country) { // This function (not provided here) will use a trained machine learning model to provide a // suitable price based on recent customer reviews. - function priceFromRecentReviews(Couchbase\QueryResult $qr) - { + $priceFromRecentReviews = function (\Couchbase\QueryResult $qr): float { // this would call a trained ML model to get the best price return 99.98; - } - $updatedPrice = priceFromRecentReviews($qr); + }; + $updatedPrice = $priceFromRecentReviews($qr); // Set the price of all hotels in the chain $ctx->query( @@ -502,6 +506,7 @@ function (TransactionAttemptContext $ctx) use ($collection, $costOfItem) { echo "\nRunning: full-error-handling example\n"; function completeErrorHandling($cluster, $collection) { + $costOfItem = 0; // tag::full-error-handling[] try { $result = $cluster->transactions()->run( diff --git a/modules/howtos/examples/using-cas.php b/modules/howtos/examples/using-cas.php index 96cd2a29..e2a7d8eb 100644 --- a/modules/howtos/examples/using-cas.php +++ b/modules/howtos/examples/using-cas.php @@ -4,7 +4,7 @@ use \Couchbase\Cluster; use \Couchbase\Collection; use \Couchbase\ReplaceOptions; -use \Couchbase\CasMismatchError; +use \Couchbase\Exception\CasMismatchException; // #tag::increment[] function incrementVisitCount(Collection $collection, string $userId) { @@ -22,7 +22,7 @@ function incrementVisitCount(Collection $collection, string $userId) { $opts = new ReplaceOptions(); $opts->cas($res->cas()); $collection->replace($userId, $user, $opts); - } catch (CasMismatchError $ex) { + } catch (CasMismatchException $ex) { continue; } @@ -62,5 +62,5 @@ function lockingAndCas(Collection $collection, string $userId) { $collection->upsert("userId", ["visit_count" => 0]); -replaceWithCas($collection, "userId"); +incrementVisitCount($collection, "userId"); lockingAndCas($collection, "userId"); diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 00000000..7517b048 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,2 @@ +parameters: + level: 0