Skip to content

Commit 9bc8999

Browse files
authored
Switch to phpcs and upgrade codebase (#2596)
To be consistent with other MongoDB PHP projects, we use phpcs to analyze and fix the code style. Summary of changes: - added "declare(strict_types=1);" to all file headers - added "use function" for all functions used in each file (performance gain for functions with specific opcode compilation) - use of short closures - remove `@var` annotations in tests, replaced by `assertInstanceOf` or `assert` - Use early exit when appropriate, but disable the rule - Remove `@param` & `@return` phpdoc when duplicate of native types
1 parent 5394a8d commit 9bc8999

File tree

90 files changed

+2101
-1901
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+2101
-1901
lines changed

Diff for: .github/workflows/build-ci.yml

-21
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,6 @@ on:
77
pull_request:
88

99
jobs:
10-
php-cs-fixer:
11-
runs-on: ubuntu-latest
12-
env:
13-
PHP_CS_FIXER_VERSION: v3.6.0
14-
strategy:
15-
matrix:
16-
php:
17-
- '8.1'
18-
steps:
19-
- name: Checkout
20-
uses: actions/checkout@v3
21-
- name: Setup PHP
22-
uses: shivammathur/setup-php@v2
23-
with:
24-
php-version: ${{ matrix.php }}
25-
extensions: curl,mbstring
26-
tools: php-cs-fixer:${{ env.PHP_CS_FIXER_VERSION }}
27-
coverage: none
28-
- name: Run PHP-CS-Fixer Fix, version ${{ env.PHP_CS_FIXER_VERSION }}
29-
run: php-cs-fixer fix --dry-run --diff --ansi
30-
3110
build:
3211
runs-on: ${{ matrix.os }}
3312
name: PHP v${{ matrix.php }} with MongoDB ${{ matrix.mongodb }}

Diff for: .github/workflows/coding-standards.yml

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: "Coding Standards"
2+
3+
on:
4+
push:
5+
branches:
6+
tags:
7+
pull_request:
8+
9+
env:
10+
PHP_VERSION: "8.2"
11+
DRIVER_VERSION: "stable"
12+
13+
jobs:
14+
phpcs:
15+
name: "phpcs"
16+
runs-on: "ubuntu-22.04"
17+
18+
steps:
19+
- name: "Checkout"
20+
uses: "actions/checkout@v3"
21+
22+
- name: Setup cache environment
23+
id: extcache
24+
uses: shivammathur/cache-extensions@v1
25+
with:
26+
php-version: ${{ env.PHP_VERSION }}
27+
extensions: "mongodb-${{ env.DRIVER_VERSION }}"
28+
key: "extcache-v1"
29+
30+
- name: Cache extensions
31+
uses: actions/cache@v3
32+
with:
33+
path: ${{ steps.extcache.outputs.dir }}
34+
key: ${{ steps.extcache.outputs.key }}
35+
restore-keys: ${{ steps.extcache.outputs.key }}
36+
37+
- name: "Install PHP"
38+
uses: "shivammathur/setup-php@v2"
39+
with:
40+
coverage: "none"
41+
extensions: "mongodb-${{ env.DRIVER_VERSION }}"
42+
php-version: "${{ env.PHP_VERSION }}"
43+
tools: "cs2pr"
44+
45+
- name: "Show driver information"
46+
run: "php --ri mongodb"
47+
48+
- name: "Install dependencies with Composer"
49+
uses: "ramsey/[email protected]"
50+
with:
51+
composer-options: "--no-suggest"
52+
53+
# The -q option is required until phpcs v4 is released
54+
- name: "Run PHP_CodeSniffer"
55+
run: "vendor/bin/phpcs -q --no-colors --report=checkstyle | cs2pr"

Diff for: .gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
.DS_Store
55
.idea/
66
.phpunit.result.cache
7-
/.php-cs-fixer.php
8-
/.php-cs-fixer.cache
7+
.phpcs-cache
98
/vendor
109
composer.lock
1110
composer.phar

Diff for: .php-cs-fixer.dist.php

-187
This file was deleted.

Diff for: composer.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"require-dev": {
3434
"phpunit/phpunit": "^9.5.10",
3535
"orchestra/testbench": "^8.0",
36-
"mockery/mockery": "^1.4.4"
36+
"mockery/mockery": "^1.4.4",
37+
"doctrine/coding-standard": "12.0.x-dev"
3738
},
3839
"replace": {
3940
"jenssegers/mongodb": "self.version"
@@ -56,5 +57,10 @@
5657
]
5758
}
5859
},
59-
"minimum-stability": "dev"
60+
"minimum-stability": "dev",
61+
"config": {
62+
"allow-plugins": {
63+
"dealerdirect/phpcodesniffer-composer-installer": true
64+
}
65+
}
6066
}

Diff for: phpcs.xml.dist

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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>src</file>
13+
<file>tests</file>
14+
15+
<!-- Target minimum supported PHP version -->
16+
<config name="php_version" value="80100"/>
17+
18+
<!-- ****************************************** -->
19+
<!-- Import rules from doctrine/coding-standard -->
20+
<!-- ****************************************** -->
21+
<rule ref="Doctrine">
22+
<exclude name="SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed"/>
23+
<exclude name="SlevomatCodingStandard.ControlStructures.JumpStatementsSpacing"/>
24+
<exclude name="SlevomatCodingStandard.ControlStructures.NewWithParentheses"/>
25+
<exclude name="SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly"/>
26+
<exclude name="SlevomatCodingStandard.Functions.ArrowFunctionDeclaration"/>
27+
<exclude name="SlevomatCodingStandard.Functions.StaticClosure"/>
28+
<exclude name="SlevomatCodingStandard.TypeHints.UnionTypeHintFormat.DisallowedShortNullable"/>
29+
30+
<!-- Model properties use snake_case -->
31+
<exclude name="Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps"/>
32+
33+
<!-- Type changes needs to be synchronized with laravel/framework -->
34+
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint"/>
35+
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint"/>
36+
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint"/>
37+
</rule>
38+
</ruleset>

Diff for: src/Auth/DatabaseTokenRepository.php

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace MongoDB\Laravel\Auth;
46

57
use DateTime;
@@ -8,11 +10,12 @@
810
use Illuminate\Support\Facades\Date;
911
use MongoDB\BSON\UTCDateTime;
1012

13+
use function date_default_timezone_get;
14+
use function is_array;
15+
1116
class DatabaseTokenRepository extends BaseDatabaseTokenRepository
1217
{
13-
/**
14-
* @inheritdoc
15-
*/
18+
/** @inheritdoc */
1619
protected function getPayload($email, $token)
1720
{
1821
return [
@@ -22,19 +25,15 @@ protected function getPayload($email, $token)
2225
];
2326
}
2427

25-
/**
26-
* @inheritdoc
27-
*/
28+
/** @inheritdoc */
2829
protected function tokenExpired($createdAt)
2930
{
3031
$createdAt = $this->convertDateTime($createdAt);
3132

3233
return parent::tokenExpired($createdAt);
3334
}
3435

35-
/**
36-
* @inheritdoc
37-
*/
36+
/** @inheritdoc */
3837
protected function tokenRecentlyCreated($createdAt)
3938
{
4039
$createdAt = $this->convertDateTime($createdAt);
@@ -50,7 +49,7 @@ private function convertDateTime($createdAt)
5049
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
5150
$createdAt = $date->format('Y-m-d H:i:s');
5251
} elseif (is_array($createdAt) && isset($createdAt['date'])) {
53-
$date = new DateTime($createdAt['date'], new DateTimeZone(isset($createdAt['timezone']) ? $createdAt['timezone'] : 'UTC'));
52+
$date = new DateTime($createdAt['date'], new DateTimeZone($createdAt['timezone'] ?? 'UTC'));
5453
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
5554
$createdAt = $date->format('Y-m-d H:i:s');
5655
}

0 commit comments

Comments
 (0)