Skip to content

Commit

Permalink
Added generic type documentation; Added static analysis with phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Sep 6, 2023
1 parent 1cd6612 commit a0923e5
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 9 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Static Analysis

on: [ push ]

jobs:
PHPUnit:
strategy:
matrix:
include:
# Laravel 10.*
- php: 8.1
laravel: 10.*
composer-flag: '--prefer-stable'
- php: 8.2
laravel: 10.*
composer-flag: '--prefer-stable'
- php: 8.1
laravel: 10.*
composer-flag: '--prefer-lowest'
- php: 8.2
laravel: 10.*
composer-flag: '--prefer-lowest'

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, iconv
coverage: none

- name: Install dependencies
run: composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update

- name: Update dependencies
run: composer update ${{ matrix.composer-flag }} --prefer-dist --no-interaction

- name: Run PhpStan
run: composer analyse
15 changes: 10 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
"illuminate/database": "^10"
},
"require-dev": {
"phpunit/phpunit": "^10",
"friendsofphp/php-cs-fixer": "^3",
"nunomaduro/larastan": "^2.0",
"orchestra/testbench": "^8.9",
"phpunit/phpunit": "^10",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
Expand All @@ -31,10 +33,13 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
"fix": "./vendor/bin/php-cs-fixer fix",
"lint": "./vendor/bin/phpcs --error-severity=1 --warning-severity=8 --extensions=php"
"test": "@php vendor/bin/phpunit",
"test-coverage": "@php vendor/bin/phpunit --coverage-html coverage",
"fix": "@php ./vendor/bin/php-cs-fixer fix",
"lint": "@php ./vendor/bin/phpcs --error-severity=1 --warning-severity=8 --extensions=php",
"analyse": [
"@php ./vendor/bin/phpstan analyse --memory-limit=2G"
]
},
"config": {
"sort-packages": true
Expand Down
11 changes: 11 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
includes:
- ./vendor/nunomaduro/larastan/extension.neon

parameters:

paths:
- src
- tests

# Level 9 is the highest level
level: 5
8 changes: 6 additions & 2 deletions src/HasManyMerged.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;

/**
* @template TRelatedModel of Model
* @extends Relation<TRelatedModel>
*/
class HasManyMerged extends Relation
{
/**
Expand All @@ -28,7 +32,7 @@ class HasManyMerged extends Relation
/**
* Create a new has one or many relationship instance.
*
* @param Builder $query
* @param Builder<TRelatedModel> $query
* @param Model $parent
* @param array $foreignKeys
* @param string $localKey
Expand Down Expand Up @@ -241,7 +245,7 @@ public function getQualifiedForeignKeyNames(): array
/**
* Get the results of the relationship.
*
* @return mixed
* @phpstan-return \Traversable<int, TRelatedModel>
*/
public function getResults()
{
Expand Down
7 changes: 5 additions & 2 deletions tests/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Korridor\LaravelHasManyMerged\HasManyMergedRelation;

/**
* @property int $id
*/
class Message extends Model
{
use HasManyMergedRelation;
Expand All @@ -26,15 +29,15 @@ class Message extends Model
];

/**
* @return BelongsTo<User>
* @return BelongsTo<User, Message>
*/
public function sender(): BelongsTo
{
return $this->belongsTo(User::class, 'sender_user_id');
}

/**
* @return BelongsTo<User>
* @return BelongsTo<User, Message>
*/
public function receiver(): BelongsTo
{
Expand Down
7 changes: 7 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
use Korridor\LaravelHasManyMerged\HasManyMerged;
use Korridor\LaravelHasManyMerged\HasManyMergedRelation;

/**
* @property int $id
* @property int $other_unique_id
* @property string $name
* @property int $messages_sum_content_integer
* @property int $messages_count
*/
class User extends Model
{
use HasManyMergedRelation;
Expand Down

0 comments on commit a0923e5

Please sign in to comment.