Skip to content

Commit

Permalink
Add Code Analysis and Code Style validation (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Jun 21, 2023
1 parent 534c66f commit cefcf39
Show file tree
Hide file tree
Showing 22 changed files with 308 additions and 76 deletions.
8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/.* export-ignore
/Makefile export-ignore
/*.dist export-ignore
/*.dist.* export-ignore
/tests export-ignore

# Test with:
# git archive --format=tar HEAD | tar t
47 changes: 47 additions & 0 deletions .github/workflows/ca.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# yamllint disable rule:line-length
# yamllint disable rule:braces

name: Code Analysis

on:
pull_request:
push:
branches:
- main

jobs:
build:
name: Static code analysis with PHP ${{ matrix.php-version }}
runs-on: ubuntu-latest

strategy:
matrix:
php-version:
- "7.4"

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: none

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: composer-${{ matrix.php-version }}-${{ hashFiles('composer.*') }}
restore-keys: |
composer-${{ matrix.php-version }}-
composer-
- name: Install dependencies
run: |
composer install --prefer-dist --no-progress
- name: Run Psalm
run: |
php vendor/bin/psalm --output-format=github --long-progress --php-version=${{ matrix.php-version }}
65 changes: 65 additions & 0 deletions .github/workflows/cs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# yamllint disable rule:line-length
# yamllint disable rule:braces

name: Code Style

on:
pull_request:
push:
branches:
- main

jobs:
build:
name: Code style with PHP ${{ matrix.php-version }}
runs-on: ubuntu-latest

strategy:
matrix:
php-version:
- "7.4"

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: none

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: composer-${{ matrix.php-version }}-${{ hashFiles('composer.*') }}
restore-keys: |
composer-${{ matrix.php-version }}-
composer-
- name: Install dependencies
run: |
composer install --prefer-dist --no-progress
- name: Validate composer.json
run: |
composer validate --strict
- name: Normalize composer.json
run: |
composer normalize --dry-run --diff
- name: Restore PHP-CS-Fixer cache
uses: actions/cache@v3
with:
path: build/cache/.php_cs.cache
key: php-cs-fixer-${{ matrix.php-version }}-${{ hashFiles('composer.*') }}
restore-keys: |
php-cs-fixer-${{ matrix.php-version }}-
php-cs-fixer-
- name: Run PHP-CS-Fixer
run: |
mkdir -p build/cache/
vendor/bin/php-cs-fixer fix --diff --dry-run --verbose --cache-file=build/cache/.php_cs.cache
1 change: 0 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ on:
push:
branches:
- main
- v*.*

jobs:
tests:
Expand Down
72 changes: 72 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types=1);
/**
* The Sliding Window Counter, a short-lived time series library.
* Copyright 2023 Automattic, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

$header = <<<'EOF'
The Sliding Window Counter, a short-lived time series library.
Copyright 2023 Automattic, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
EOF;

$config = new PhpCsFixer\Config();
$config
->setRiskyAllowed(true)
->setRules([
'header_comment' => [
'comment_type' => 'PHPDoc',
'header' => $header,
'separate' => 'bottom',
'location' => 'after_declare_strict',
],
'@PER-CS1.0' => true,
'array_indentation' => true,
'native_function_invocation' => [
'include' => ['@internal'],
'scope' => 'namespaced',
],
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
'import_functions' => true,
],
'declare_equal_normalize' => ['space' => 'none'],
'blank_line_after_opening_tag' => false,
'linebreak_after_opening_tag' => false,
])
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
->append([__FILE__])
)
;

return $config;
18 changes: 18 additions & 0 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@
*/

declare(strict_types=1);
/**
* The Sliding Window Counter, a short-lived time series library.
* Copyright 2023 Automattic, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

require 'vendor/autoload.php';

Expand Down
16 changes: 16 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<file name="example.php" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
10 changes: 5 additions & 5 deletions src/AnomalyDetectionResult.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
<?php declare(strict_types=1);
/**
* The Sliding Window Counter, a short-lived time series library.
* Copyright 2023 Automattic, Inc.
*
* This program is free software; you can redistribute it and/or
Expand All @@ -12,12 +13,11 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; If not, see <http://www.gnu.org/licenses/>.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

declare(strict_types=1);

namespace Automattic\SlidingWindowCounter;

use function abs;
Expand Down
10 changes: 5 additions & 5 deletions src/Cache/CounterCache.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
<?php declare(strict_types=1);
/**
* The Sliding Window Counter, a short-lived time series library.
* Copyright 2023 Automattic, Inc.
*
* This program is free software; you can redistribute it and/or
Expand All @@ -12,12 +13,11 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; If not, see <http://www.gnu.org/licenses/>.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

declare(strict_types=1);

namespace Automattic\SlidingWindowCounter\Cache;

interface CounterCache
Expand Down
11 changes: 6 additions & 5 deletions src/Cache/MemcachedAdapter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
<?php declare(strict_types=1);
/**
* The Sliding Window Counter, a short-lived time series library.
* Copyright 2023 Automattic, Inc.
*
* This program is free software; you can redistribute it and/or
Expand All @@ -12,15 +13,15 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; If not, see <http://www.gnu.org/licenses/>.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

declare(strict_types=1);

namespace Automattic\SlidingWindowCounter\Cache;

use Memcached;

use function implode;
use function is_int;

Expand Down
11 changes: 6 additions & 5 deletions src/Cache/WPCacheAdapter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
<?php declare(strict_types=1);
/**
* The Sliding Window Counter, a short-lived time series library.
* Copyright 2023 Automattic, Inc.
*
* This program is free software; you can redistribute it and/or
Expand All @@ -12,15 +13,15 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; If not, see <http://www.gnu.org/licenses/>.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

declare(strict_types=1);

namespace Automattic\SlidingWindowCounter\Cache;

use WP_Object_Cache;

use function is_int;

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Helper/Frame.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
<?php declare(strict_types=1);
/**
* The Sliding Window Counter, a short-lived time series library.
* Copyright 2023 Automattic, Inc.
*
* This program is free software; you can redistribute it and/or
Expand All @@ -12,12 +13,11 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; If not, see <http://www.gnu.org/licenses/>.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

declare(strict_types=1);

namespace Automattic\SlidingWindowCounter\Helper;

use function implode;
Expand Down
Loading

0 comments on commit cefcf39

Please sign in to comment.