Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added generic type documentation; Added static analysis with phpstan #15

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ class User extends Model
// ...

/**
* @return HasManyMerged|Message
* @return HasManyMerged<Message>
*/
public function messages(): HasManyMerged
{
return $this->hasManyMerged(Message::class, ['sender_user_id', 'receiver_user_id']);
}

/**
* @return HasMany|Message
* @return HasMany<Message>
*/
public function sentMessages(): HasMany
{
return $this->hasMany(Message::class, 'sender_user_id');
}

/**
* @return HasMany|Message
* @return HasMany<Message>
*/
public function receivedMessages(): HasMany
{
Expand Down
14 changes: 9 additions & 5 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,9 +32,9 @@ 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[] $foreignKeys
* @param string $localKey
* @return void
*/
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 All @@ -251,8 +255,8 @@ public function getResults()
/**
* Execute the query as a "select" statement.
*
* @param array $columns
* @return \Illuminate\Database\Eloquent\Collection
* @param string[] $columns
* @return Collection<int, TRelatedModel>
*/
public function get($columns = ['*'])
{
Expand Down
4 changes: 3 additions & 1 deletion src/HasManyMergedRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Korridor\LaravelHasManyMerged;

use Illuminate\Database\Eloquent\Model;

trait HasManyMergedRelation
{
/**
* @param string $related
* @param class-string $related
* @param string[]|null $foreignKeys
* @param string|null $localKey
* @return HasManyMerged
Expand Down
16 changes: 8 additions & 8 deletions tests/HasManyMergedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testHasManyMergedWithTwoUsersWereBothAreSenderOrReceiverOfTheSam
$user2 = User::find(12);
$messagesOfUser1 = $user1->messages;
$messagesOfUser2 = $user2->messages;
$queries = $this->db::getQueryLog();
$queries = $this->db::connection()->getQueryLog();
$this->db::connection()->disableQueryLog();

// Assert
Expand All @@ -90,7 +90,7 @@ public function testHasManyMergedWithTwoUsersWereBothAreSenderOrReceiverOfTheSam
$user2 = $users->firstWhere('id', 12);
$messagesOfUser1 = $user1->messages;
$messagesOfUser2 = $user2->messages;
$queries = $this->db::getQueryLog();
$queries = $this->db::connection()->getQueryLog();
$this->db::connection()->disableQueryLog();

// Assert
Expand All @@ -115,7 +115,7 @@ public function testHasManyMergedWithTwoUsersWereBothAreSenderOrReceiverOfTheSam
$user2 = $users->firstWhere('id', 12);
$messagesOfUser1 = $user1->messages;
$messagesOfUser2 = $user2->messages;
$queries = $this->db::getQueryLog();
$queries = $this->db::connection()->getQueryLog();
$this->db::connection()->disableQueryLog();

// Assert
Expand Down Expand Up @@ -146,7 +146,7 @@ public function testHasManyMergedWithTwoUsersWereBothAreSenderOrReceiverOfTheSam
$user2 = $users->firstWhere('id', 12);
$messagesOfUser1 = $user1->messages;
$messagesOfUser2 = $user2->messages;
$queries = $this->db::getQueryLog();
$queries = $this->db::connection()->getQueryLog();
$this->db::connection()->disableQueryLog();

// Assert
Expand All @@ -171,7 +171,7 @@ public function testHasManyMergedWithTwoUsersWereBothAreSenderOrReceiverOfTheSam
$users = User::with(['messages'])->whereHas('messages', function (Builder $builder): void {
$builder->where('content_integer', '=', 2);
})->get();
$queries = $this->db::getQueryLog();
$queries = $this->db::connection()->getQueryLog();
$this->db::connection()->disableQueryLog();

// Assert
Expand All @@ -192,7 +192,7 @@ public function testHasManyMergedWithTwoUsersWereBothAreSenderOrReceiverOfTheSam
$user2 = $users->firstWhere('id', 12);
$messagesOfUser1 = $user1->messages;
$messagesOfUser2 = $user2->messages;
$queries = $this->db::getQueryLog();
$queries = $this->db::connection()->getQueryLog();
$this->db::connection()->disableQueryLog();

// Assert
Expand Down Expand Up @@ -221,7 +221,7 @@ public function testHasManyMergedWithTwoUsersWereBothAreSenderOrReceiverOfTheSam
$user2 = $users->firstWhere('id', 12);
$messagesOfUser1 = $user1->messages;
$messagesOfUser2 = $user2->messages;
$queries = $this->db::getQueryLog();
$queries = $this->db::connection()->getQueryLog();
$this->db::connection()->disableQueryLog();

// Assert
Expand Down Expand Up @@ -250,7 +250,7 @@ public function testHasManyMergedWithTwoUsersWereBothAreSenderOrReceiverOfTheSam
$user2 = $users->firstWhere('id', 12);
$messagesOfUser1 = $user1->messages;
$messagesOfUser2 = $user2->messages;
$queries = $this->db::getQueryLog();
$queries = $this->db::connection()->getQueryLog();
$this->db::connection()->disableQueryLog();

// Assert
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
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

abstract class TestCase extends PHPUnitTestCase
{
protected $db;
protected Manager $db;

protected function setUp(): void
{
Expand Down