Skip to content

Commit 1c719fe

Browse files
authored
Merge pull request #111 from mongodb/DOCSP-42819-add-coding-standard-wkflw
DOCSP-42819: add coding standards workflow
2 parents 101bc6c + 00c1a2a commit 1c719fe

File tree

9 files changed

+384
-1
lines changed

9 files changed

+384
-1
lines changed

.github/actions/setup/action.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Setup
2+
description: Sets up the build environment
3+
inputs:
4+
php-version:
5+
description: "PHP version to install"
6+
required: true
7+
driver-version:
8+
description: "MongoDB extension version to install"
9+
required: true
10+
php-ini-values:
11+
description: "INI values to pass along to setup-php action"
12+
required: false
13+
default: ""
14+
15+
runs:
16+
using: composite
17+
steps:
18+
- name: Setup cache environment
19+
id: extcache
20+
uses: shivammathur/cache-extensions@v1
21+
with:
22+
php-version: ${{ inputs.php-version }}
23+
extensions: "mongodb-${{ inputs.driver-version }}"
24+
key: "extcache-v1"
25+
26+
- name: Cache extensions
27+
uses: actions/cache@v4
28+
with:
29+
path: ${{ steps.extcache.outputs.dir }}
30+
key: ${{ steps.extcache.outputs.key }}
31+
restore-keys: ${{ steps.extcache.outputs.key }}
32+
33+
- name: Install PHP
34+
uses: shivammathur/setup-php@v2
35+
with:
36+
coverage: none
37+
extensions: "mongodb-${{ inputs.driver-version }}"
38+
php-version: "${{ inputs.php-version }}"
39+
tools: cs2pr
40+
ini-values: "${{ inputs.php-ini-values }}"
41+
42+
- name: Show driver information
43+
run: "php --ri mongodb"
44+
shell: bash
45+
46+
- name: Install dependencies with Composer
47+
uses: ramsey/composer-install@3.0.0
48+
with:
49+
# Revert when psalm supports PHP 8.4
50+
# composer-options: "--no-suggest"
51+
composer-options: "--no-suggest ${{ inputs.php-version == '8.4'
52+
&& '--ignore-platform-req=php+' || '' }}"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: "Coding Standards"
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- "source/**/*.php"
7+
- ".github/workflows/*.yml"
8+
9+
env:
10+
PHP_VERSION: "8.2"
11+
# TODO: change to "stable" once 1.20.0 is released
12+
# DRIVER_VERSION: "stable"
13+
DRIVER_VERSION: "mongodb/mongo-php-driver@master"
14+
15+
jobs:
16+
phpcs:
17+
name: "phpcs"
18+
runs-on: "ubuntu-22.04"
19+
20+
steps:
21+
- name: "Checkout"
22+
uses: "actions/checkout@v4"
23+
24+
- name: "Setup"
25+
uses: "./.github/actions/setup"
26+
with:
27+
php-version: ${{ env.PHP_VERSION }}
28+
driver-version: ${{ env.DRIVER_VERSION }}
29+
30+
# The -q option is required until phpcs v4 is released
31+
- name: "Run PHP_CodeSniffer"
32+
run: "vendor/bin/phpcs -q --no-colors --report=checkstyle | cs2pr"

.github/workflows/static-analysis.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: "Static Analysis"
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- "source/**/*.php"
7+
- ".github/workflows/*.yml"
8+
workflow_call:
9+
inputs:
10+
ref:
11+
description: "The git ref to check"
12+
type: string
13+
required: true
14+
15+
env:
16+
PHP_VERSION: "8.2"
17+
# TODO: change to "stable" once 1.20.0 is released
18+
# DRIVER_VERSION: "stable"
19+
DRIVER_VERSION: "mongodb/mongo-php-driver@master"
20+
21+
jobs:
22+
psalm:
23+
name: "Psalm"
24+
runs-on: "ubuntu-22.04"
25+
26+
steps:
27+
- name: "Checkout"
28+
uses: "actions/checkout@v4"
29+
with:
30+
ref: ${{ github.event_name == 'workflow_dispatch' && inputs.ref || github.ref }}
31+
32+
- name: "Get SHA hash of checked out ref"
33+
if: ${{ github.event_name == 'workflow_dispatch' }}
34+
run: |
35+
echo CHECKED_OUT_SHA=$(git rev-parse HEAD) >> $GITHUB_ENV
36+
37+
- name: "Setup"
38+
uses: "./.github/actions/setup"
39+
with:
40+
php-version: ${{ env.PHP_VERSION }}
41+
driver-version: ${{ env.DRIVER_VERSION }}
42+
43+
- name: "Run Psalm"
44+
run: "vendor/bin/psalm --show-info=false --stats --output-format=github --threads=$(nproc) --report=psalm.sarif"
45+
46+
- name: "Upload SARIF report"
47+
if: ${{ github.event_name != 'workflow_dispatch' }}
48+
uses: "github/codeql-action/upload-sarif@v3"
49+
with:
50+
sarif_file: psalm.sarif
51+
52+
- name: "Upload SARIF report"
53+
if: ${{ github.event_name == 'workflow_dispatch' }}
54+
uses: "github/codeql-action/upload-sarif@v3"
55+
with:
56+
sarif_file: psalm.sarif
57+
ref: ${{ inputs.ref }}
58+
sha: ${{ env.CHECKED_OUT_SHA }}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ build
55
*giza.log
66
source/*
77
backups/*
8+
vendor/
9+
composer.lock
10+
.phpcs-cache

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "mongodb/docs-php-library",
3+
"description": "MongoDB PHP Library Documentation",
4+
"require": {
5+
"ext-mongodb": "^1.19",
6+
"mongodb/mongodb": "^1.19"
7+
},
8+
"require-dev": {
9+
"doctrine/coding-standard": "^12.0",
10+
"squizlabs/php_codesniffer": "^3.7",
11+
"vimeo/psalm": "^5.13"
12+
},
13+
"config": {
14+
"allow-plugins": {
15+
"dealerdirect/phpcodesniffer-composer-installer": true
16+
}
17+
}
18+
}

phpcs.xml

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?xml version="1.0"?>
2+
<ruleset>
3+
<arg name="basepath" value="." />
4+
<arg name="extensions" value="php" />
5+
<arg name="parallel" value="80" />
6+
<arg name="cache" value=".phpcs-cache" />
7+
<arg name="colors" />
8+
9+
<!-- Ignore warnings (n), show progress of the run (p), and show sniff names (s) -->
10+
<arg value="nps"/>
11+
12+
<file>source/examples</file>
13+
14+
<!-- Target minimum supported PHP version -->
15+
<config name="php_version" value="80100"/>
16+
17+
<!-- ****************************************** -->
18+
<!-- Import rules from doctrine/coding-standard -->
19+
<!-- ****************************************** -->
20+
<rule ref="Doctrine">
21+
<!-- No namespace for examples -->
22+
<exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace" />
23+
24+
<!-- Can cause subtle BC breaks -->
25+
<exclude name="SlevomatCodingStandard.TypeHints.DeclareStrictTypes" />
26+
27+
28+
<!-- **************************************** -->
29+
<!-- Exclude sniffs that force unwanted style -->
30+
<!-- **************************************** -->
31+
<exclude name="Generic.Formatting.MultipleStatementAlignment" />
32+
<exclude name="Squiz.Commenting.FunctionComment.ThrowsNoFullStop" />
33+
<exclude name="SlevomatCodingStandard.TypeHints.UnionTypeHintFormat.DisallowedShortNullable" />
34+
35+
<!-- ********************* -->
36+
<!-- Exclude broken sniffs -->
37+
<!-- ********************* -->
38+
39+
<!-- Sniff currently broken when casting arrays, see https://github.com/squizlabs/PHP_CodeSniffer/issues/2937#issuecomment-615498860 -->
40+
<exclude name="Squiz.Arrays.ArrayDeclaration.ValueNoNewline" />
41+
42+
<!-- Disable forbidden annotation sniff as excluding @api from the list doesn't work -->
43+
<exclude name="SlevomatCodingStandard.Commenting.ForbiddenAnnotations.AnnotationForbidden" />
44+
45+
<!-- Example file using HTML templating -->
46+
<exclude name="Generic.Files.InlineHTML.Found"/>
47+
<exclude name="SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing.IncorrectLinesCountAfterControlStructure"/>
48+
</rule>
49+
50+
51+
<!-- **************************************** -->
52+
<!-- Enable rules not enforced by Doctrine CS -->
53+
<!-- **************************************** -->
54+
55+
<!-- Forbid fully qualified names even for colliding names -->
56+
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly">
57+
<properties>
58+
<property name="allowFallbackGlobalConstants" value="false"/>
59+
<property name="allowFallbackGlobalFunctions" value="false"/>
60+
<property name="allowFullyQualifiedGlobalClasses" value="false"/>
61+
<property name="allowFullyQualifiedGlobalConstants" value="false"/>
62+
<property name="allowFullyQualifiedGlobalFunctions" value="false"/>
63+
<property phpcs-only="true" name="allowFullyQualifiedNameForCollidingClasses" value="false"/>
64+
<property phpcs-only="true" name="allowFullyQualifiedNameForCollidingConstants" value="false"/>
65+
<property phpcs-only="true" name="allowFullyQualifiedNameForCollidingFunctions" value="false"/>
66+
<property name="searchAnnotations" value="true"/>
67+
</properties>
68+
</rule>
69+
70+
71+
<!-- ****************************************************** -->
72+
<!-- Don't require annotations to specify traversable types -->
73+
<!-- ****************************************************** -->
74+
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint">
75+
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification" />
76+
</rule>
77+
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint">
78+
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingTraversableTypeHintSpecification" />
79+
</rule>
80+
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint">
81+
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification" />
82+
</rule>
83+
84+
85+
<!-- ************************************************************************** -->
86+
<!-- Require type hints for all parameters, properties, and return types in src -->
87+
<!-- ************************************************************************** -->
88+
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingAnyTypeHint">
89+
<exclude-pattern>tests</exclude-pattern>
90+
</rule>
91+
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint">
92+
<exclude-pattern>tests</exclude-pattern>
93+
</rule>
94+
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint">
95+
<exclude-pattern>tests</exclude-pattern>
96+
</rule>
97+
98+
99+
<!-- *********************************************************** -->
100+
<!-- Require native type hints for all code without a BC promise -->
101+
<!-- *********************************************************** -->
102+
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint">
103+
<exclude-pattern>src</exclude-pattern>
104+
</rule>
105+
106+
107+
<!-- ************************************************************* -->
108+
<!-- Ignore errors for certain files where this is part of the API -->
109+
<!-- ************************************************************* -->
110+
<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
111+
<exclude-pattern>/src/GridFS/StreamWrapper</exclude-pattern>
112+
<exclude-pattern>/tests/DocumentationExamplesTest.php</exclude-pattern>
113+
<exclude-pattern>/tests/GridFS/UnusableStream.php</exclude-pattern>
114+
<exclude-pattern>/tests/SpecTests/ClientSideEncryption/Prose*</exclude-pattern>
115+
</rule>
116+
<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
117+
<exclude-pattern>/examples</exclude-pattern>
118+
<exclude-pattern>/tests/PHPUnit/ConstraintTrait.php</exclude-pattern>
119+
</rule>
120+
<rule ref="Squiz.Classes.ClassFileName.NoMatch">
121+
<exclude-pattern>/examples</exclude-pattern>
122+
</rule>
123+
<rule ref="Squiz.Classes.ValidClassName.NotCamelCaps">
124+
<exclude-pattern>/tests/SpecTests/ClientSideEncryption/Prose*</exclude-pattern>
125+
</rule>
126+
</ruleset>

psalm-baseline.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<files psalm-version="5.25.0@01a8eb06b9e9cc6cfb6a320bf9fb14331919d505">
3+
<file src="source/examples/aws-lambda/index.php">
4+
<MissingFile>
5+
<code><![CDATA[require_once __DIR__ . '/vendor/autoload.php']]></code>
6+
</MissingFile>
7+
<MixedAssignment>
8+
<code><![CDATA[$planet]]></code>
9+
</MixedAssignment>
10+
<PossiblyFalseArgument>
11+
<code><![CDATA[$uri]]></code>
12+
</PossiblyFalseArgument>
13+
</file>
14+
<file src="source/examples/codecs/handling-data-types/DateTimeCodec.php">
15+
<MissingTemplateParam>
16+
<code><![CDATA[DecodeIfSupported]]></code>
17+
<code><![CDATA[EncodeIfSupported]]></code>
18+
</MissingTemplateParam>
19+
<MixedAssignment>
20+
<code><![CDATA[$dateTime]]></code>
21+
</MixedAssignment>
22+
<MixedMethodCall>
23+
<code><![CDATA[get]]></code>
24+
<code><![CDATA[get]]></code>
25+
<code><![CDATA[getName]]></code>
26+
<code><![CDATA[getTimezone]]></code>
27+
<code><![CDATA[setTimeZone]]></code>
28+
<code><![CDATA[toDateTime]]></code>
29+
</MixedMethodCall>
30+
</file>
31+
<file src="source/examples/codecs/handling-documents/disabling-codec.php">
32+
<MixedMethodCall>
33+
<code><![CDATA[aggregate]]></code>
34+
<code><![CDATA[findOne]]></code>
35+
</MixedMethodCall>
36+
<PossiblyUndefinedVariable>
37+
<code><![CDATA[$filter]]></code>
38+
<code><![CDATA[$pipeline]]></code>
39+
</PossiblyUndefinedVariable>
40+
<UndefinedGlobalVariable>
41+
<code><![CDATA[$collection]]></code>
42+
<code><![CDATA[$collection]]></code>
43+
</UndefinedGlobalVariable>
44+
</file>
45+
<file src="source/examples/codecs/handling-embedded-documents/AddressCodec.php">
46+
<MissingTemplateParam>
47+
<code><![CDATA[DecodeIfSupported]]></code>
48+
<code><![CDATA[EncodeIfSupported]]></code>
49+
</MissingTemplateParam>
50+
<MixedMethodCall>
51+
<code><![CDATA[get]]></code>
52+
<code><![CDATA[get]]></code>
53+
<code><![CDATA[get]]></code>
54+
<code><![CDATA[get]]></code>
55+
</MixedMethodCall>
56+
</file>
57+
</files>

psalm.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="1"
4+
errorBaseline="psalm-baseline.xml"
5+
resolveFromConfigFile="true"
6+
findUnusedBaselineEntry="true"
7+
findUnusedCode="false"
8+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xmlns="https://getpsalm.org/schema/config"
10+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
11+
>
12+
<projectFiles>
13+
<directory name="source/examples" />
14+
<ignoreFiles>
15+
<directory name="vendor" />
16+
</ignoreFiles>
17+
</projectFiles>
18+
<!--
19+
<stubs>
20+
<file name="stubs/BSON/Document.stub.php"/>
21+
<file name="stubs/BSON/Iterator.stub.php"/>
22+
<file name="stubs/BSON/PackedArray.stub.php"/>
23+
</stubs>
24+
-->
25+
<issueHandlers>
26+
<!-- This is often the result of type checks due to missing native types -->
27+
<DocblockTypeContradiction errorLevel="info" />
28+
29+
<!-- If the result of getenv is falsy, using the default URI is fine -->
30+
<RiskyTruthyFalsyComparison errorLevel="suppress"/>
31+
32+
<!-- The same class can be defined in multiple examples -->
33+
<DuplicateClass errorLevel="suppress" />
34+
<MixedArgument errorLevel="suppress" />
35+
<MixedPropertyFetch errorLevel="suppress" />
36+
</issueHandlers>
37+
</psalm>

0 commit comments

Comments
 (0)