diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.yml new file mode 100644 index 00000000..61d350f3 --- /dev/null +++ b/.github/actions/setup/action.yml @@ -0,0 +1,52 @@ +name: Setup +description: Sets up the build environment +inputs: + php-version: + description: "PHP version to install" + required: true + driver-version: + description: "MongoDB extension version to install" + required: true + php-ini-values: + description: "INI values to pass along to setup-php action" + required: false + default: "" + +runs: + using: composite + steps: + - name: Setup cache environment + id: extcache + uses: shivammathur/cache-extensions@v1 + with: + php-version: ${{ inputs.php-version }} + extensions: "mongodb-${{ inputs.driver-version }}" + key: "extcache-v1" + + - name: Cache extensions + uses: actions/cache@v4 + with: + path: ${{ steps.extcache.outputs.dir }} + key: ${{ steps.extcache.outputs.key }} + restore-keys: ${{ steps.extcache.outputs.key }} + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + coverage: none + extensions: "mongodb-${{ inputs.driver-version }}" + php-version: "${{ inputs.php-version }}" + tools: cs2pr + ini-values: "${{ inputs.php-ini-values }}" + + - name: Show driver information + run: "php --ri mongodb" + shell: bash + + - name: Install dependencies with Composer + uses: ramsey/composer-install@3.0.0 + with: + # Revert when psalm supports PHP 8.4 + # composer-options: "--no-suggest" + composer-options: "--no-suggest ${{ inputs.php-version == '8.4' + && '--ignore-platform-req=php+' || '' }}" diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml new file mode 100644 index 00000000..30012e46 --- /dev/null +++ b/.github/workflows/coding-standards.yml @@ -0,0 +1,32 @@ +name: "Coding Standards" + +on: + pull_request: + paths: + - "source/**/*.php" + - ".github/workflows/*.yml" + +env: + PHP_VERSION: "8.2" + # TODO: change to "stable" once 1.20.0 is released + # DRIVER_VERSION: "stable" + DRIVER_VERSION: "mongodb/mongo-php-driver@master" + +jobs: + phpcs: + name: "phpcs" + runs-on: "ubuntu-22.04" + + steps: + - name: "Checkout" + uses: "actions/checkout@v4" + + - name: "Setup" + uses: "./.github/actions/setup" + with: + php-version: ${{ env.PHP_VERSION }} + driver-version: ${{ env.DRIVER_VERSION }} + + # The -q option is required until phpcs v4 is released + - name: "Run PHP_CodeSniffer" + run: "vendor/bin/phpcs -q --no-colors --report=checkstyle | cs2pr" diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml new file mode 100644 index 00000000..ef58d7c5 --- /dev/null +++ b/.github/workflows/static-analysis.yml @@ -0,0 +1,58 @@ +name: "Static Analysis" + +on: + pull_request: + paths: + - "source/**/*.php" + - ".github/workflows/*.yml" + workflow_call: + inputs: + ref: + description: "The git ref to check" + type: string + required: true + +env: + PHP_VERSION: "8.2" + # TODO: change to "stable" once 1.20.0 is released + # DRIVER_VERSION: "stable" + DRIVER_VERSION: "mongodb/mongo-php-driver@master" + +jobs: + psalm: + name: "Psalm" + runs-on: "ubuntu-22.04" + + steps: + - name: "Checkout" + uses: "actions/checkout@v4" + with: + ref: ${{ github.event_name == 'workflow_dispatch' && inputs.ref || github.ref }} + + - name: "Get SHA hash of checked out ref" + if: ${{ github.event_name == 'workflow_dispatch' }} + run: | + echo CHECKED_OUT_SHA=$(git rev-parse HEAD) >> $GITHUB_ENV + + - name: "Setup" + uses: "./.github/actions/setup" + with: + php-version: ${{ env.PHP_VERSION }} + driver-version: ${{ env.DRIVER_VERSION }} + + - name: "Run Psalm" + run: "vendor/bin/psalm --show-info=false --stats --output-format=github --threads=$(nproc) --report=psalm.sarif" + + - name: "Upload SARIF report" + if: ${{ github.event_name != 'workflow_dispatch' }} + uses: "github/codeql-action/upload-sarif@v3" + with: + sarif_file: psalm.sarif + + - name: "Upload SARIF report" + if: ${{ github.event_name == 'workflow_dispatch' }} + uses: "github/codeql-action/upload-sarif@v3" + with: + sarif_file: psalm.sarif + ref: ${{ inputs.ref }} + sha: ${{ env.CHECKED_OUT_SHA }} diff --git a/.gitignore b/.gitignore index b42a4f24..c50788aa 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ build *giza.log source/* backups/* +vendor/ +composer.lock +.phpcs-cache diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..3efdceab --- /dev/null +++ b/composer.json @@ -0,0 +1,18 @@ +{ + "name": "mongodb/docs-php-library", + "description": "MongoDB PHP Library Documentation", + "require": { + "ext-mongodb": "^1.19", + "mongodb/mongodb": "^1.19" + }, + "require-dev": { + "doctrine/coding-standard": "^12.0", + "squizlabs/php_codesniffer": "^3.7", + "vimeo/psalm": "^5.13" + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } + } +} diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 00000000..b7a82547 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,126 @@ + + + + + + + + + + + + source/examples + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + tests + + + tests + + + tests + + + + + + + + src + + + + + + + + /src/GridFS/StreamWrapper + /tests/DocumentationExamplesTest.php + /tests/GridFS/UnusableStream.php + /tests/SpecTests/ClientSideEncryption/Prose* + + + /examples + /tests/PHPUnit/ConstraintTrait.php + + + /examples + + + /tests/SpecTests/ClientSideEncryption/Prose* + + diff --git a/psalm-baseline.xml b/psalm-baseline.xml new file mode 100644 index 00000000..19357242 --- /dev/null +++ b/psalm-baseline.xml @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 00000000..dc1d7d11 --- /dev/null +++ b/psalm.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/source/examples/codecs/handling-embedded-documents/Person.php b/source/examples/codecs/handling-embedded-documents/Person.php index a2440d54..9367c27e 100644 --- a/source/examples/codecs/handling-embedded-documents/Person.php +++ b/source/examples/codecs/handling-embedded-documents/Person.php @@ -8,7 +8,7 @@ final class Person public function __construct( public string $name, - public readonly ObjectId $id = new ObjectId() + public readonly ObjectId $id = new ObjectId(), ) { } }