Skip to content

Commit

Permalink
Switch to phpcs and upgrade codebase (#2596)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
GromNaN authored Sep 6, 2023
1 parent 5394a8d commit 9bc8999
Show file tree
Hide file tree
Showing 90 changed files with 2,101 additions and 1,901 deletions.
21 changes: 0 additions & 21 deletions .github/workflows/build-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,6 @@ on:
pull_request:

jobs:
php-cs-fixer:
runs-on: ubuntu-latest
env:
PHP_CS_FIXER_VERSION: v3.6.0
strategy:
matrix:
php:
- '8.1'
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl,mbstring
tools: php-cs-fixer:${{ env.PHP_CS_FIXER_VERSION }}
coverage: none
- name: Run PHP-CS-Fixer Fix, version ${{ env.PHP_CS_FIXER_VERSION }}
run: php-cs-fixer fix --dry-run --diff --ansi

build:
runs-on: ${{ matrix.os }}
name: PHP v${{ matrix.php }} with MongoDB ${{ matrix.mongodb }}
Expand Down
55 changes: 55 additions & 0 deletions .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: "Coding Standards"

on:
push:
branches:
tags:
pull_request:

env:
PHP_VERSION: "8.2"
DRIVER_VERSION: "stable"

jobs:
phpcs:
name: "phpcs"
runs-on: "ubuntu-22.04"

steps:
- name: "Checkout"
uses: "actions/checkout@v3"

- name: Setup cache environment
id: extcache
uses: shivammathur/cache-extensions@v1
with:
php-version: ${{ env.PHP_VERSION }}
extensions: "mongodb-${{ env.DRIVER_VERSION }}"
key: "extcache-v1"

- name: Cache extensions
uses: actions/cache@v3
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-${{ env.DRIVER_VERSION }}"
php-version: "${{ env.PHP_VERSION }}"
tools: "cs2pr"

- name: "Show driver information"
run: "php --ri mongodb"

- name: "Install dependencies with Composer"
uses: "ramsey/[email protected]"
with:
composer-options: "--no-suggest"

# The -q option is required until phpcs v4 is released
- name: "Run PHP_CodeSniffer"
run: "vendor/bin/phpcs -q --no-colors --report=checkstyle | cs2pr"
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
.DS_Store
.idea/
.phpunit.result.cache
/.php-cs-fixer.php
/.php-cs-fixer.cache
.phpcs-cache
/vendor
composer.lock
composer.phar
Expand Down
187 changes: 0 additions & 187 deletions .php-cs-fixer.dist.php

This file was deleted.

10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"require-dev": {
"phpunit/phpunit": "^9.5.10",
"orchestra/testbench": "^8.0",
"mockery/mockery": "^1.4.4"
"mockery/mockery": "^1.4.4",
"doctrine/coding-standard": "12.0.x-dev"
},
"replace": {
"jenssegers/mongodb": "self.version"
Expand All @@ -56,5 +57,10 @@
]
}
},
"minimum-stability": "dev"
"minimum-stability": "dev",
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
38 changes: 38 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0"?>
<ruleset>
<arg name="basepath" value="." />
<arg name="extensions" value="php" />
<arg name="parallel" value="80" />
<arg name="cache" value=".phpcs-cache" />
<arg name="colors" />

<!-- Ignore warnings (n), show progress of the run (p), and show sniff names (s) -->
<arg value="nps"/>

<file>src</file>
<file>tests</file>

<!-- Target minimum supported PHP version -->
<config name="php_version" value="80100"/>

<!-- ****************************************** -->
<!-- Import rules from doctrine/coding-standard -->
<!-- ****************************************** -->
<rule ref="Doctrine">
<exclude name="SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed"/>
<exclude name="SlevomatCodingStandard.ControlStructures.JumpStatementsSpacing"/>
<exclude name="SlevomatCodingStandard.ControlStructures.NewWithParentheses"/>
<exclude name="SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly"/>
<exclude name="SlevomatCodingStandard.Functions.ArrowFunctionDeclaration"/>
<exclude name="SlevomatCodingStandard.Functions.StaticClosure"/>
<exclude name="SlevomatCodingStandard.TypeHints.UnionTypeHintFormat.DisallowedShortNullable"/>

<!-- Model properties use snake_case -->
<exclude name="Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps"/>

<!-- Type changes needs to be synchronized with laravel/framework -->
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint"/>
</rule>
</ruleset>
19 changes: 9 additions & 10 deletions src/Auth/DatabaseTokenRepository.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace MongoDB\Laravel\Auth;

use DateTime;
Expand All @@ -8,11 +10,12 @@
use Illuminate\Support\Facades\Date;
use MongoDB\BSON\UTCDateTime;

use function date_default_timezone_get;
use function is_array;

class DatabaseTokenRepository extends BaseDatabaseTokenRepository
{
/**
* @inheritdoc
*/
/** @inheritdoc */
protected function getPayload($email, $token)
{
return [
Expand All @@ -22,19 +25,15 @@ protected function getPayload($email, $token)
];
}

/**
* @inheritdoc
*/
/** @inheritdoc */
protected function tokenExpired($createdAt)
{
$createdAt = $this->convertDateTime($createdAt);

return parent::tokenExpired($createdAt);
}

/**
* @inheritdoc
*/
/** @inheritdoc */
protected function tokenRecentlyCreated($createdAt)
{
$createdAt = $this->convertDateTime($createdAt);
Expand All @@ -50,7 +49,7 @@ private function convertDateTime($createdAt)
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
$createdAt = $date->format('Y-m-d H:i:s');
} elseif (is_array($createdAt) && isset($createdAt['date'])) {
$date = new DateTime($createdAt['date'], new DateTimeZone(isset($createdAt['timezone']) ? $createdAt['timezone'] : 'UTC'));
$date = new DateTime($createdAt['date'], new DateTimeZone($createdAt['timezone'] ?? 'UTC'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
$createdAt = $date->format('Y-m-d H:i:s');
}
Expand Down
Loading

0 comments on commit 9bc8999

Please sign in to comment.