Skip to content

Commit

Permalink
Automalically check and fix coding-style
Browse files Browse the repository at this point in the history
  • Loading branch information
alies-dev committed Apr 14, 2023
1 parent 0743ea0 commit fe1d64e
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 23 deletions.
95 changes: 95 additions & 0 deletions .github/workflows/format_php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: Format (PHP)

on:
push:
paths:
- '**.php'
- 'composer.*'
- 'phpcs.xml'

jobs:
format_php:
name: Format PHP
runs-on: ubuntu-latest
timeout-minutes: 7
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}

# mtime needs to be restored for PHP-CS-Fixer cache to work correctly
- name: Restore mtimes
uses: weirdan/git-restore-mtime-action@master

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
coverage: none
tools: cs2pr
env:
COMPOSER_TOKEN: ${{ secrets.GH_TOKEN_FOR_COMPOSER_FOR_PRIVATE_REPOS }}

- name: Get Composer cache directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Retrieve Composer‘s cache
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Install composer dependencies -- step 1
run: 'composer install --working-dir=tools/php-cs-fixer --no-interaction --no-progress --no-scripts'

- name: Install composer dependencies -- step 2
run: 'composer install --working-dir=tools/phpcs --no-interaction --no-progress --no-scripts'

- name: Retrieve PHP-CS-Fixer‘s cache
uses: actions/cache@v3
with:
path: .php-cs-fixer.cache
key: ${{ runner.os }}-php-cs-fixer-${{ hashFiles('.php-cs-fixer.php', '.phpcs/**/**') }}
restore-keys: |
${{ runner.os }}-php-cs-fixer-
- name: Retrieve PHPCS‘s cache
uses: actions/cache@v3
with:
path: .phpcs.cache
key: ${{ runner.os }}-phpcs-${{ hashFiles('phpcs.xml') }}
restore-keys: |
${{ runner.os }}-phpcs-
- name: Detect PHP coding style issues
id: lint_php
run: composer php:lint
continue-on-error: true

- name: Fix detected PHP coding style issues (if any)
if: ${{ steps.lint_php.outcome == 'failure' }}
id: fix_php
run: composer php:fix
continue-on-error: true

- name: Commit PHP code-style fixes (if any)
if: ${{ steps.lint_php.outcome == 'failure' }}
uses: EndBug/add-and-commit@v9
with:
message: '#15000 🪄️ Apply coding style fixes to PHP'
committer_name: GitHub Actions
committer_email: [email protected]
add: '*.php'
pull: '--rebase --autostash'

- name: Lint PHP coding style issues (if previously detected)
if: ${{ steps.lint_php.outcome == 'failure' }}
run: composer phpcs -- --report-full --report-checkstyle=./phpcs-report.xml

- name: Show PHPCS results in on GitHub UI
if: ${{ steps.lint_php.outcome == 'failure' }}
run: cs2pr ./phpcs-report.xml
21 changes: 14 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "interaction-design-foundation/nova-html-card",
"description": "A Laravel Nova card to display arbitrary HTML content",
"license": "MIT",
"keywords": [
"laravel",
"nova",
Expand All @@ -9,21 +10,24 @@
"text",
"markdown"
],
"license": "MIT",
"require": {
"php": ">=8.0",
"laravel/nova": "^4.20"
},
"require-dev": {
"interaction-design-foundation/coding-standard": "^0.0.4",
"orchestra/testbench": "^7.0",
"phpunit/phpunit": "^9.6 || ^10.0",
"vimeo/psalm": "^5.6"
},
"repositories": [
{
"type": "composer", "url": "https://nova.laravel.com"
"type": "composer",
"url": "https://nova.laravel.com"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
"InteractionDesignFoundation\\HtmlCard\\": "src/"
Expand All @@ -34,19 +38,22 @@
"InteractionDesignFoundation\\HtmlCard\\Tests\\": "tests"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
},
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"InteractionDesignFoundation\\HtmlCard\\CardServiceProvider"
]
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"cs:check": "phpcs -p -s --colors --report-full --report-summary",
"cs:fix": "phpcbf -p --colors",
"test": "phpunit --colors=always"
}
}
21 changes: 21 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<ruleset name="My Coding Standard">
<!-- Include all rules from the IxDF Coding Standard -->
<rule ref="IxDFCodingStandard">
<exclude ref="IxDFCodingStandard.Files.BemCasedFilename.InvalidCharacters"/>
<exclude ref="SlevomatCodingStandard.Classes.RequireAbstractOrFinal.ClassNeitherAbstractNorFinal"/>
<exclude ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName.NoMatchBetweenTypeNameAndFileName"/>
</rule>

<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
<exclude-pattern>./tests*</exclude-pattern>
</rule>

<rule ref="Generic.Files.LineLength.TooLong">
<severity>1</severity><!-- Temp hide the warn -->
</rule>

<!-- Paths to check -->
<file>src</file>
<file>tests</file>
</ruleset>
8 changes: 4 additions & 4 deletions src/CardServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace InteractionDesignFoundation\HtmlCard;

use Laravel\Nova\Nova;
use Laravel\Nova\Events\ServingNova;
use Illuminate\Support\ServiceProvider;
use Laravel\Nova\Events\ServingNova;
use Laravel\Nova\Nova;

class CardServiceProvider extends ServiceProvider
class CardServiceProvider extends ServiceProvider // phpcs:ignore SlevomatCodingStandard.Classes.RequireAbstractOrFinal.ClassNeitherAbstractNorFinal
{
/**
* Bootstrap any application services.
* @return void
*/
public function boot()
{
Nova::serving(function (ServingNova $event) {
Nova::serving(static function (ServingNova $event) {
Nova::script('html-card', __DIR__.'/../dist/js/card.js');
});
}
Expand Down
7 changes: 3 additions & 4 deletions src/HtmlCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Support\Facades\App;
use Laravel\Nova\Card;

class HtmlCard extends Card
class HtmlCard extends Card // phpcs:ignore SlevomatCodingStandard.Classes.RequireAbstractOrFinal.ClassNeitherAbstractNorFinal
{
/**
* The width of the card (1/3, 1/2, or full).
Expand All @@ -15,9 +15,8 @@ class HtmlCard extends Card

/**
* Create a new element.
* @param string|null $component
*/
public function __construct($component = null)
public function __construct(string | null $component = null)
{
parent::__construct($component);

Expand Down Expand Up @@ -54,8 +53,8 @@ public function markdown(string $markdownContent): static

/**
* Use blade view file to render Card content.
* @param string $view
* @param array<string, mixed> $viewData
* @phpcsSuppress SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint
*/
public function view(string $view, array $viewData = []): static
{
Expand Down
1 change: 0 additions & 1 deletion src/LaravelMarkdownConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

final class LaravelMarkdownConverter extends Markdown implements MarkdownConverter
{

}
1 change: 1 addition & 0 deletions src/MarkdownConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface MarkdownConverter
* Parse the given Markdown text into HTML.
* @param string $text
* @return \Illuminate\Support\HtmlString
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*/
public static function parse($text);
}
2 changes: 1 addition & 1 deletion tests/CardServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace InteractionDesignFoundation\HtmlCard\Tests;

use InteractionDesignFoundation\HtmlCard\MarkdownConverter;
use Illuminate\Support\Facades\App;
use InteractionDesignFoundation\HtmlCard\MarkdownConverter;

/** @covers \InteractionDesignFoundation\HtmlCard\CardServiceProvider */
final class CardServiceProviderTest extends TestCase
Expand Down
7 changes: 1 addition & 6 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@

abstract class TestCase extends OrchestraTestCase
{
public function setUp(): void
{
parent::setUp();
}

/** @inheritDoc */
protected function getPackageProviders($app): array
protected function getPackageProviders(): array
{
return [
CardServiceProvider::class,
Expand Down

0 comments on commit fe1d64e

Please sign in to comment.