From c66f5be842d14f910f08d9879c765fa717c9a998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20K=C3=BCbler?= Date: Wed, 19 May 2021 08:55:00 +0200 Subject: [PATCH 1/6] add workflows to project --- .github/workflows/lint.yaml | 31 ++++++++++++++++++++++++++++ .github/workflows/test.yaml | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 .github/workflows/lint.yaml create mode 100644 .github/workflows/test.yaml diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 0000000..797950f --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,31 @@ +name: "Lint PHP" +on: + pull_request: + push: + branches: + - master + - develop +jobs: + phpcs: + name: PHPCS + runs-on: ubuntu-20.04 + strategy: + matrix: + php-versions: ['7.4'] + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + extensions: mbstring, curl, zip + coverage: none + tools: composer + + - name: Install dependencies + run: composer update --no-interaction --no-suggest --no-progress --optimize-autoloader + + - name: Run + run: composer run lint:phpcs \ No newline at end of file diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..de14081 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,40 @@ +name: "Test PHP" +on: + pull_request: + push: + branches: + - master + - develop +jobs: + phpunit: + name: PHPUnit + runs-on: ubuntu-20.04 + strategy: + matrix: + php-versions: ['7.3', '7.4', '8.0'] + composer-arguments: ['--prefer-lowest', ''] + allowed-to-fail: [false] + include: + - php-versions: '8.1' + composer-arguments: ['--prefer-lowest', ''] + allowed-to-fail: true + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + extensions: mbstring, curl, zip + coverage: xdebug + tools: composer + + - name: Install dependencies + continue-on-error: ${{ matrix.allowed-to-fail }} + run: composer update --no-interaction --no-suggest --no-progress --optimize-autoloader --quiet ${{ matrix.composer-arguments }} + + - name: Run tests + continue-on-error: ${{ matrix.allowed-to-fail }} + run: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover \ No newline at end of file From 7024adc3b94a18ba1022a3d8199d9a12ca2ec556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20K=C3=BCbler?= Date: Wed, 19 May 2021 08:57:42 +0200 Subject: [PATCH 2/6] use phpmd at workflows --- .github/workflows/lint.yaml | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 797950f..acc72c8 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -28,4 +28,27 @@ jobs: run: composer update --no-interaction --no-suggest --no-progress --optimize-autoloader - name: Run - run: composer run lint:phpcs \ No newline at end of file + run: composer run lint:phpcs + phpmd: + name: PHPMD + runs-on: ubuntu-20.04 + strategy: + matrix: + php-versions: ['7.4'] + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + extensions: mbstring, curl, zip + coverage: none + tools: composer + + - name: Install dependencies + run: composer update --no-interaction --no-suggest --no-progress --optimize-autoloader + + - name: Run + run: composer run lint:phpmd \ No newline at end of file From 4c0eb81ad9bcf74082906c073864b553c3190d53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20K=C3=BCbler?= Date: Wed, 19 May 2021 08:58:06 +0200 Subject: [PATCH 3/6] remove travis config --- .travis.yml | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 6783b22..0000000 --- a/.travis.yml +++ /dev/null @@ -1,25 +0,0 @@ -language: php - -php: - - 7.3 - - 7.4 - - nightly - -matrix: - include: - - php: 7.3 - env: COMPOSER_FLAGS="--prefer-lowest" - - php: 7.4 - env: COMPOSER_FLAGS="--prefer-lowest" - - php: 7.4 - allow_failures: - - php: nightly - -before_script: - - travis_retry composer self-update - - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source - -script: - - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover - - vendor/bin/phpcs -s - - vendor/bin/phpmd config,src,tests text phpmd.xml From 8c459aa2876e8dd14774c243bf9605e2f3d08f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20K=C3=BCbler?= Date: Wed, 19 May 2021 09:11:10 +0200 Subject: [PATCH 4/6] remove --quit flag --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index de14081..00c324b 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -33,7 +33,7 @@ jobs: - name: Install dependencies continue-on-error: ${{ matrix.allowed-to-fail }} - run: composer update --no-interaction --no-suggest --no-progress --optimize-autoloader --quiet ${{ matrix.composer-arguments }} + run: composer update --no-interaction --no-suggest --no-progress --optimize-autoloader ${{ matrix.composer-arguments }} - name: Run tests continue-on-error: ${{ matrix.allowed-to-fail }} From 571fcaa1561dd33d0276930d2083a6be25ab241b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20K=C3=BCbler?= Date: Wed, 19 May 2021 09:17:15 +0200 Subject: [PATCH 5/6] remove consistence/coding-standard to support php 8 --- composer.json | 1 - phpcs.xml | 381 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 322 insertions(+), 60 deletions(-) diff --git a/composer.json b/composer.json index a772f0a..d627028 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,6 @@ "phpunit/phpunit": "^9.3", "phpmd/phpmd": "^2.6", "squizlabs/php_codesniffer": "^3.3", - "consistence/coding-standard": "^3.8", "slevomat/coding-standard": "^6.3" }, "autoload": { diff --git a/phpcs.xml b/phpcs.xml index 3bf2912..7b3633f 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,68 +1,331 @@ - - The coding standard for this project. + + + - - config - src - tests + The coding standard for this project. - *.blade.php$ + + src + tests - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + - - */tests/* - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */tests/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Only 1 @return annotation is allowed in a function comment + + + Extra @param annotation + + + Function has no return statement, but annotation @return is present + + + @param annotation for parameter "%s" missing + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Variable "%s" not allowed in double quoted string; use sprintf() instead + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 85c3ac2f117363c81b2357e4ca3510ce66a332fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20K=C3=BCbler?= Date: Wed, 19 May 2021 09:55:43 +0200 Subject: [PATCH 6/6] use new linter rules --- phpcs.xml | 1 + src/Middlewares/RoleMiddleware.php | 2 -- src/PermissionServiceProvider.php | 2 -- src/Traits/HasPermissions.php | 14 ++++++-------- src/Traits/HasRoles.php | 2 -- tests/BladeTest.php | 4 +--- tests/MatchPermissionTest.php | 2 -- tests/MatchRuleToPermissionTest.php | 2 -- tests/MiddlewareTest.php | 6 ++---- tests/PermissionTest.php | 2 -- tests/RoleTest.php | 2 -- tests/TestCase.php | 4 ++-- tests/User.php | 13 ++++++++++--- 13 files changed, 22 insertions(+), 34 deletions(-) diff --git a/phpcs.xml b/phpcs.xml index 7b3633f..c4278eb 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -9,6 +9,7 @@ src tests + */tests/resources/*.blade.php diff --git a/src/Middlewares/RoleMiddleware.php b/src/Middlewares/RoleMiddleware.php index a0e8150..2f5e482 100644 --- a/src/Middlewares/RoleMiddleware.php +++ b/src/Middlewares/RoleMiddleware.php @@ -7,7 +7,6 @@ class RoleMiddleware { - /** * Handle an incoming request. * @@ -31,5 +30,4 @@ public function handle(Request $request, Closure $next, string $roles) return $next($request); } - } diff --git a/src/PermissionServiceProvider.php b/src/PermissionServiceProvider.php index 5bf41c6..7330104 100644 --- a/src/PermissionServiceProvider.php +++ b/src/PermissionServiceProvider.php @@ -7,7 +7,6 @@ class PermissionServiceProvider extends ServiceProvider { - /** * Bootstrap the application services. */ @@ -84,5 +83,4 @@ protected function registerBladeExtensions(): void }); }); } - } diff --git a/src/Traits/HasPermissions.php b/src/Traits/HasPermissions.php index 74abefe..3478abd 100644 --- a/src/Traits/HasPermissions.php +++ b/src/Traits/HasPermissions.php @@ -6,11 +6,10 @@ trait HasPermissions { - /** * Check if model has all permissions * - * @param array|string ...$permissions + * @param string[]|string ...$permissions * @return bool */ public function hasPermissionTo(...$permissions): bool @@ -35,7 +34,7 @@ public function hasPermissionTo(...$permissions): bool /** * Check if model has all permissions * - * @param array|string ...$permissions + * @param string[]|string ...$permissions * @return bool */ public function hasPermission(...$permissions): bool @@ -46,7 +45,7 @@ public function hasPermission(...$permissions): bool /** * Check if model has any permissions * - * @param array|string ...$permissions + * @param string[]|string ...$permissions * @return bool */ public function hasAnyPermission(...$permissions): bool @@ -87,7 +86,7 @@ public function getAllPermissions(): Collection * Match ruleset to permission * * @param \Illuminate\Support\Collection $rules Ruleset - * @param array $permission + * @param string[] $permission * @return bool */ public function matchPermission(Collection $rules, array $permission): bool @@ -111,8 +110,8 @@ public function matchPermission(Collection $rules, array $permission): bool /** * Match one rule to permission * - * @param array $rule - * @param array $permission + * @param string[] $rule + * @param string[] $permission * @return bool */ public function matchRuleToPermission(array $rule, array $permission): bool @@ -146,5 +145,4 @@ public function matchRuleToPermission(array $rule, array $permission): bool return $countPermissionParts === $countRuleParts; } - } diff --git a/src/Traits/HasRoles.php b/src/Traits/HasRoles.php index aa8fac0..3850c1c 100644 --- a/src/Traits/HasRoles.php +++ b/src/Traits/HasRoles.php @@ -4,7 +4,6 @@ trait HasRoles { - use HasPermissions; /** @@ -48,5 +47,4 @@ public function getRoleName(): ?string { return $this->{config('permission.column_name')}; } - } diff --git a/tests/BladeTest.php b/tests/BladeTest.php index 9b3f15c..b072641 100644 --- a/tests/BladeTest.php +++ b/tests/BladeTest.php @@ -6,7 +6,6 @@ class BladeTest extends TestCase { - public function testPermission(): void { $this->user->assignRole('admin'); @@ -71,7 +70,7 @@ protected function setUp(): void * return compiled blade views * * @param string $view view name - * @param array $parameters vars can used in the view + * @param string[] $parameters vars can used in the view */ protected function renderView(string $view, array $parameters): string { @@ -83,5 +82,4 @@ protected function renderView(string $view, array $parameters): string return trim((string) $view); } - } diff --git a/tests/MatchPermissionTest.php b/tests/MatchPermissionTest.php index 907d1a9..99ec215 100644 --- a/tests/MatchPermissionTest.php +++ b/tests/MatchPermissionTest.php @@ -4,7 +4,6 @@ class MatchPermissionTest extends TestCase { - public function testNoRules(): void { $rules = collect([]); @@ -61,5 +60,4 @@ protected function setUp(): void ]; $this->user = User::create(['email' => 'test@user.com']); } - } diff --git a/tests/MatchRuleToPermissionTest.php b/tests/MatchRuleToPermissionTest.php index b1bb53c..e594ed9 100644 --- a/tests/MatchRuleToPermissionTest.php +++ b/tests/MatchRuleToPermissionTest.php @@ -4,7 +4,6 @@ class MatchRuleToPermissionTest extends TestCase { - public function testDirectRule(): void { $rule = [ @@ -96,5 +95,4 @@ protected function setUp(): void ]; $this->user = User::create(['email' => 'test@user.com']); } - } diff --git a/tests/MiddlewareTest.php b/tests/MiddlewareTest.php index 101153a..fe50ae1 100644 --- a/tests/MiddlewareTest.php +++ b/tests/MiddlewareTest.php @@ -10,7 +10,6 @@ class MiddlewareTest extends TestCase { - public function testGustCannotAccessProtectedRoute(): void { $this->assertEquals($this->runMiddleware($this->roleMiddleware, 'admin'), 403); @@ -53,12 +52,11 @@ protected function setUp(): void protected function runMiddleware(RoleMiddleware $middleware, string $parameter): int { try { - return $middleware->handle(new Request, static function () { - return (new Response)->SetContent(''); + return $middleware->handle(new Request(), static function () { + return (new Response())->SetContent(''); }, $parameter)->status(); } catch (HttpException $e) { return $e->getStatusCode(); } } - } diff --git a/tests/PermissionTest.php b/tests/PermissionTest.php index 90038ef..15a15b8 100644 --- a/tests/PermissionTest.php +++ b/tests/PermissionTest.php @@ -5,7 +5,6 @@ /** @SuppressWarnings(PHPMD.TooManyPublicMethods) */ class PermissionTest extends TestCase { - public function testGetPermissions(): void { $this->user->assignRole('user'); @@ -130,5 +129,4 @@ protected function setUp(): void $this->user = User::create(['email' => 'test@user.com']); } - } diff --git a/tests/RoleTest.php b/tests/RoleTest.php index 564d6e0..1f2d86a 100644 --- a/tests/RoleTest.php +++ b/tests/RoleTest.php @@ -4,7 +4,6 @@ class RoleTest extends TestCase { - public function testAssignExistentRole(): void { $this->user->assignRole('admin'); @@ -60,5 +59,4 @@ protected function setUp(): void $this->user = User::create(['email' => 'test@user.com']); } - } diff --git a/tests/TestCase.php b/tests/TestCase.php index 4b67365..206905d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -9,7 +9,6 @@ class TestCase extends OrchestraTestCase { - /** * Setup the test environment. * @@ -29,6 +28,7 @@ protected function setUp(): void * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification + * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint */ protected function getPackageProviders($app): array { @@ -40,6 +40,7 @@ protected function getPackageProviders($app): array * * @param \Illuminate\Foundation\Application $app * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification + * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint */ protected function getEnvironmentSetUp($app): void { @@ -71,5 +72,4 @@ protected function setUpDatabase(Application $app): void $table->string('role')->nullable(); }); } - } diff --git a/tests/User.php b/tests/User.php index e19d341..4cc9e58 100644 --- a/tests/User.php +++ b/tests/User.php @@ -11,20 +11,27 @@ class User extends Model implements AuthorizableContract, AuthenticatableContract { - use HasRoles; use Authorizable; use Authenticatable; + /** + * @var bool + * @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint + */ public $timestamps = false; /** * The attributes that are mass assignable. * - * @var array + * @var string[] + * @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint */ protected $fillable = ['email']; + /** + * @var string + * @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint + */ protected $table = 'users'; - }