Skip to content

Commit

Permalink
chore: add phpcs and phpstan to workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
jshah4517 committed May 29, 2024
1 parent 10064b6 commit 1f530aa
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 71 deletions.
92 changes: 51 additions & 41 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,56 @@
name: CI

on:
push:
pull_request:
push:
pull_request:

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
php: [8.1, 8.2, 8.3]
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: pecl
extensions: xdebug
- name: Show php version
run: php -v && composer -V
- name: Debug if needed
run: if [[ "$DEBUG" == "true" ]]; then env; fi
env:
DEBUG: ${{secrets.DEBUG}}
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ matrix.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ matrix.os }}-composer-
- name: Install dependencies
run: composer install --prefer-source
- name: Run unit tests
run: ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
- name: Run Coverage
run: ./vendor/bin/php-coveralls -v
env:
COVERALLS_RUN_LOCALLY: ${{ secrets.COVERALLS_RUN_LOCALLY }}
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
continue-on-error: true
- name: show coverage json
run: cat build/logs/coveralls-upload.json
build:
runs-on: ubuntu-latest
strategy:
matrix:
php: [8.1, 8.2, 8.3]
stability: [prefer-stable]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }}

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup php
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: pecl
extensions: xdebug

- name: Show php version
run: php -v && composer -V

- name: Debug if needed
run: if [[ "$DEBUG" == "true" ]]; then env; fi
env:
DEBUG: ${{secrets.DEBUG}}

- name: Install dependencies
run: composer install --prefer-source

- name: PHP code standard
run: php vendor/bin/phpcs --standard=phpcs.xml src/ tests/

- name: PHP static analysis
run: php vendor/bin/phpstan analyse

- name: Run unit tests
run: ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml

- name: Run coverage
run: ./vendor/bin/php-coveralls -v
env:
COVERALLS_RUN_LOCALLY: ${{ secrets.COVERALLS_RUN_LOCALLY }}
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
continue-on-error: true

- name: Show coverage json
run: cat build/logs/coveralls-upload.json
11 changes: 10 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.7.0",
"phpunit/phpunit": "^10.0"
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.0",
"slevomat/coding-standard": "^8.15",
"squizlabs/php_codesniffer": "^3.5",
"supportpal/coding-standard": "^0.4"
},
"autoload": {
"psr-4": {
Expand All @@ -26,5 +30,10 @@
},
"autoload-dev": {
"classmap": ["tests/stubs"]
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
5 changes: 5 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<ruleset name="CustomSupportPal">
<description>Custom PHPCS config based on the SupportPal coding standard.</description>
<rule ref="vendor/supportpal/coding-standard" />
</ruleset>
101 changes: 101 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#1 \\$callback of function call_user_func_array expects callable\\(\\)\\: mixed, array\\{static\\(Jenssegers\\\\Model\\\\Model\\), string\\} given\\.$#"
count: 1
path: src/Model.php

-
message: "#^Unsafe usage of new static\\(\\)\\.$#"
count: 4
path: src/Model.php

-
message: "#^Access to an undefined property Jenssegers\\\\Model\\\\Model\\:\\:\\$name\\.$#"
count: 1
path: tests/ModelTest.php

-
message: "#^Access to an undefined property ModelStub\\:\\:\\$active\\.$#"
count: 1
path: tests/ModelTest.php

-
message: "#^Access to an undefined property ModelStub\\:\\:\\$age\\.$#"
count: 1
path: tests/ModelTest.php

-
message: "#^Access to an undefined property ModelStub\\:\\:\\$bar\\.$#"
count: 1
path: tests/ModelTest.php

-
message: "#^Access to an undefined property ModelStub\\:\\:\\$birthday\\.$#"
count: 1
path: tests/ModelTest.php

-
message: "#^Access to an undefined property ModelStub\\:\\:\\$city\\.$#"
count: 3
path: tests/ModelTest.php

-
message: "#^Access to an undefined property ModelStub\\:\\:\\$collection_data\\.$#"
count: 1
path: tests/ModelTest.php

-
message: "#^Access to an undefined property ModelStub\\:\\:\\$count\\.$#"
count: 1
path: tests/ModelTest.php

-
message: "#^Access to an undefined property ModelStub\\:\\:\\$data\\.$#"
count: 1
path: tests/ModelTest.php

-
message: "#^Access to an undefined property ModelStub\\:\\:\\$default\\.$#"
count: 1
path: tests/ModelTest.php

-
message: "#^Access to an undefined property ModelStub\\:\\:\\$foo\\.$#"
count: 4
path: tests/ModelTest.php

-
message: "#^Access to an undefined property ModelStub\\:\\:\\$list_items\\.$#"
count: 2
path: tests/ModelTest.php

-
message: "#^Access to an undefined property ModelStub\\:\\:\\$name\\.$#"
count: 10
path: tests/ModelTest.php

-
message: "#^Access to an undefined property ModelStub\\:\\:\\$object_data\\.$#"
count: 1
path: tests/ModelTest.php

-
message: "#^Access to an undefined property ModelStub\\:\\:\\$password\\.$#"
count: 2
path: tests/ModelTest.php

-
message: "#^Access to an undefined property ModelStub\\:\\:\\$score\\.$#"
count: 1
path: tests/ModelTest.php

-
message: "#^Access to an undefined property ModelStub\\:\\:\\$secret\\.$#"
count: 2
path: tests/ModelTest.php

-
message: "#^Parameter \\#1 \\$callback of static method Jenssegers\\\\Model\\\\Model\\:\\:unguarded\\(\\) expects callable\\(\\)\\: mixed, array\\{PHPUnit\\\\Framework\\\\MockObject\\\\MockObject&stdClass, 'callback'\\} given\\.$#"
count: 1
path: tests/ModelTest.php
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
includes:
- phpstan-baseline.neon

parameters:
level: 8
paths:
- src
- tests
parallel:
processTimeout: 300.0
35 changes: 32 additions & 3 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,58 @@
use Illuminate\Support\Collection as BaseCollection;
use JsonSerializable;

/**
* @implements ArrayAccess<string, mixed>
* @implements Arrayable<string, mixed>
*/
abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
{
/**
* The model's attributes.
*
* @var mixed[]
*/
protected array $attributes = [];

/**
* The attributes that should be hidden for arrays.
*
* @var string[]
*/
protected array $hidden = [];

/**
* The attributes that should be visible in arrays.
*
* @var string[]
*/
protected array $visible = [];

/**
* The accessors to append to the model's array form.
*
* @var string[]
*/
protected array $appends = [];

/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected array $fillable = [];

/**
* The attributes that aren't mass assignable.
*
* @var string[]
*/
protected array $guarded = [];

/**
* The attributes that should be casted to native types.
*
* @var array<string, mixed>
*/
protected array $casts = [];

Expand All @@ -57,11 +75,15 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab

/**
* The cache of the mutated attributes for each class.
*
* @var mixed[]
*/
protected static array $mutatorCache = [];

/**
* Create a new Eloquent model instance.
*
* @param mixed[] $attributes
*/
public function __construct(array $attributes = [])
{
Expand Down Expand Up @@ -379,8 +401,9 @@ public function totallyGuarded(): bool
* Convert the model instance to JSON.
*
* @param int $options
* @return string|false
*/
public function toJson($options = 0): string
public function toJson($options = 0)
{
return json_encode($this->jsonSerialize(), $options);
}
Expand Down Expand Up @@ -821,8 +844,14 @@ public static function __callStatic(string $method, array $parameters)
/**
* Convert the model to its string representation.
*/
public function __toString(): string
public function __toString()
{
return $this->toJson();
$string = $this->toJson();

if ($string === false) {
return '';
}

return $string;
}
}
Loading

0 comments on commit 1f530aa

Please sign in to comment.