Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/workflows/check-examples.yml
Original file line number Diff line number Diff line change
@@ -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' ' ')



3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"require": {
"couchbase/couchbase": "^4.0"
},
"require-dev": {
"phpstan/phpstan": "^2.2"
}
}
104 changes: 91 additions & 13 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions modules/howtos/examples/kv-crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
3 changes: 3 additions & 0 deletions modules/howtos/examples/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use \Couchbase\ConjunctionSearchQuery;
use \Couchbase\SearchOptions;
use \Couchbase\MutationState;
use \Couchbase\SearchRequest;
use \Couchbase\VectorSearch;
use \Couchbase\VectorQuery;

/*
* index definition
Expand Down
2 changes: 1 addition & 1 deletion modules/howtos/examples/subdoc-mutatein-arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
13 changes: 9 additions & 4 deletions modules/howtos/examples/transactions-example.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions modules/howtos/examples/using-cas.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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");
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
level: 0
Loading