Skip to content

Commit

Permalink
Improved code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Helldar committed Feb 10, 2022
1 parent fa56056 commit 12bc0b6
Show file tree
Hide file tree
Showing 15 changed files with 312 additions and 48 deletions.
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ ij_php_align_key_value_pairs = true
ij_php_align_match_arm_bodies = true
ij_php_align_multiline_array_initializer_expression = true
ij_php_align_multiline_binary_operation = true
ij_php_align_multiline_chained_methods = true
ij_php_align_multiline_chained_methods = false
ij_php_align_multiline_extends_list = true
ij_php_align_multiline_for = false
ij_php_align_multiline_parameters = true
Expand Down Expand Up @@ -686,7 +686,7 @@ ij_php_spaces_around_equality_operators = true
ij_php_spaces_around_logical_operators = true
ij_php_spaces_around_multiplicative_operators = true
ij_php_spaces_around_null_coalesce_operator = true
ij_php_spaces_around_pipe_in_union_type = true
ij_php_spaces_around_pipe_in_union_type = false
ij_php_spaces_around_relational_operators = true
ij_php_spaces_around_shift_operators = true
ij_php_spaces_around_unary_operator = false
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
.idea/
vendor/

*.bak
*.cache
*.clover
*.orig

composer.lock
6 changes: 1 addition & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ FROM helldar/laravel-gitlab-ci:${PHP_VERSION}
ARG INPUT_FIX
ARG INPUT_GITHUB_TOKEN

RUN composer global require friendsofphp/php-cs-fixer
RUN composer global require dragon-code/codestyler

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

COPY .php-cs.php /.php-cs.php
COPY .editorconfig /.editorconfig
COPY dependabot.php /dependabot.php

ENTRYPOINT ["/entrypoint.sh"]
60 changes: 49 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,48 @@
[![Unstable Version][badge_unstable]][link_repo]
[![License][badge_license]][link_license]

## Installation

### Required

- PHP: ^8.0
- Composer: ^2.0

### Locally

```bash
composer global require dragon-code/codestyler
```

## Usage

### Check
### CLI

#### Check code-style

```bash
codestyler check
```

#### Fix code-style

```bash
codestyler fix
```

#### Update `.editorconfig`

```bash
codestyler editorconfig
```

#### Enable Dependabot

```bash
codestyler dependabot
```

### GitHub Action

Create a new `.github/workflows/lint-check.yml` file and add the content to it:

Expand All @@ -26,12 +65,12 @@ jobs:
uses: actions/checkout@v2

- name: Checking PHP Syntax
uses: TheDragonCode/php-codestyler@v1.5.5
uses: TheDragonCode/php-codestyler@v1.6.0
```
### Fixer
Create a new `.github/workflows/lint-check.yml` file and add the content to it:
Create a new `.github/workflows/lint-fixer.yml` file and add the content to it:

```yaml
name: "Code-Style Fix"
Expand All @@ -49,26 +88,23 @@ jobs:
uses: actions/checkout@v2
- name: Checking PHP Syntax
uses: TheDragonCode/php-codestyler@v1.5.5
uses: TheDragonCode/php-codestyler@v1.6.0
with:
fix: true
```

## Configuration

By default, the linter scans the `.` with except `vendor`, `node_modules` and `.github` folders.
By default, the linter scans all files in the current launch folder, except for folders such as `vendor`, `node_modules` and `.github`.

```yaml
- uses: TheDragonCode/php-codestyler@v1.5.5
- uses: TheDragonCode/php-codestyler@v1.6.0
```

Also, by default, the linter only checks for compliance without making changes to the files.

If you want to apply changes to repository, then use the following example:
By default, the linter only checks the code-style. If you want to apply the changes, then you need to activate this option:

```yaml
- uses: TheDragonCode/php-codestyler@v1.5.5
- uses: TheDragonCode/php-codestyler@v1.6.0
with:
fix: true
```
Expand All @@ -83,6 +119,8 @@ If the `.github/dependabot.yml` file has already been created, we will check it
> Note
>
> Files will be created only if you have specified `fix: true`.
>
> Or you can manually run the Dependabot rule creation script by executing the `codestyler dependabot` command.

## License

Expand Down
51 changes: 51 additions & 0 deletions bin/codestyler
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env php
<?php

error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);

use DragonCode\CodeStyler\Application;
use DragonCode\CodeStyler\Services\Check;
use DragonCode\CodeStyler\Services\Dependabot;
use DragonCode\CodeStyler\Services\EditorConfig;
use DragonCode\CodeStyler\Services\Fix;

$possible_files = [
__DIR__ . '/../../../autoload.php',
__DIR__ . '/../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
];

$file = null;

foreach ($possible_files as $possible_file) {
if (file_exists($possible_file)) {
$file = $possible_file;
break;
}
}

if (is_null($file)) {
throw new RuntimeException('Unable to locate autoload.php file.');
}

require_once $file;

$script = $argv[1] ?? null;

$processor = match ($script) {
'check' => Check::class,
'fix' => Fix::class,
'dependabot' => Dependabot::class,
'editorconfig' => EditorConfig::class,
default => null
};

if (empty($processor)) {
$script = empty($script) ? '(empty)' : $script;

echo 'ERROR: ⚠️Unknown script parameter: ' . $script;

exit(1);
}

Application::make()->process($processor);
27 changes: 27 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "dragon-code/codestyler",
"description": "A tool to automatically fix PHP Coding Standards issues by Dragon Code.",
"license": "MIT",
"type": "library",
"authors": [
{
"name": "Andrey Helldar",
"email": "helldar@ai-rus.com"
}
],
"require": {
"php": "^8.0",
"ext-yaml": "*",
"dragon-code/support": "^5.7",
"friendsofphp/php-cs-fixer": "^3.6"
},
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"DragonCode\\CodeStyler\\": "src"
}
},
"bin": [
"bin/codestyler"
]
}
4 changes: 2 additions & 2 deletions .php-cs.php → config/rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use PhpCsFixer\Finder;

$finder = Finder::create()
->exclude(['.git', '.github', 'vendor', 'node_modules'])
->in('.');
->exclude(['.git', '.github', 'vendor', 'node_modules'])
->in('.');

return (new Config())
->setFinder($finder)
Expand Down
16 changes: 5 additions & 11 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Check only
if [[ "$INPUT_FIX" != 'true' && "$INPUT_FIX" != true ]]; then
php-cs-fixer fix --config=/.php-cs.php --dry-run --diff --ansi
codestyler check
exitcode=$?

exit $exitcode
Expand All @@ -20,22 +20,16 @@ git config --local user.name "GitHub Action"

# Copy config file
IS_DIRTY_CONFIG=1

cp -fr /.editorconfig ./.editorconfig

{ git add . && git commit -a -m "Update .editorconfig"; } || IS_DIRTY_CONFIG=0
{ codestyler editorconfig && git add . && git commit -a -m "Update .editorconfig"; } || IS_DIRTY_CONFIG=0

# Set dependabot
IS_DIRTY_DEPENDABOT=1

{ php /dependabot.php && git add . && git commit -a -m "Enabled dependabot"; } || IS_DIRTY_DEPENDABOT=0
{ codestyler dependabot && git add . && git commit -a -m "Enabled dependabot"; } || IS_DIRTY_DEPENDABOT=0

# Fix codestyle
IS_DIRTY_CODE=1

php-cs-fixer fix --config=/.php-cs.php --ansi -v

codestyler fix
{ git add . && git commit -a -m "Update code-style"; } || IS_DIRTY_CODE=0

# Push changes
if [[ "$IS_DIRTY_CONFIG" == 1 || "$IS_DIRTY_CODE" == 1 || "$IS_DIRTY_DEPENDABOT" == 1 ]]; then git push; fi
if [[ "$IS_DIRTY_CONFIG" == 1 || "$IS_DIRTY_DEPENDABOT" == 1 || "$IS_DIRTY_CODE" == 1 ]]; then git push; fi
23 changes: 23 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace DragonCode\CodeStyler;

use DragonCode\CodeStyler\Contracts\Processor;
use DragonCode\Support\Concerns\Makeable;

class Application
{
use Makeable;

public function process(string $class): void
{
$this->resolve($class)->run();
}

protected function resolve(string $class): Processor
{
return new $class();
}
}
10 changes: 10 additions & 0 deletions src/Contracts/Processor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace DragonCode\CodeStyler\Contracts;

interface Processor
{
public function run(): void;
}
13 changes: 13 additions & 0 deletions src/Services/Check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace DragonCode\CodeStyler\Services;

class Check extends CodeStyler
{
protected array $options_check = [
'--dry-run' => true,
'--diff' => true,
];
}
71 changes: 71 additions & 0 deletions src/Services/CodeStyler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace DragonCode\CodeStyler\Services;

use Composer\XdebugHandler\XdebugHandler;
use DragonCode\CodeStyler\Contracts\Processor;
use DragonCode\Support\Facades\Helpers\Ables\Arrayable;
use PhpCsFixer\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;

abstract class CodeStyler implements Processor
{
protected const ENV_PREFIX = 'PHP_CS_FIXER';

protected array $options = [
'path' => __DIR__,
'fix' => true,
'--config' => __DIR__ . '/../../config/rules.php',
'--ansi' => true,
];

protected array $options_check = [];

public function run(): void
{
$this->xdebug();
$this->styler();
}

protected function xdebug(): void
{
$xdebug = new XdebugHandler(self::ENV_PREFIX);
$xdebug->check();

unset($xdebug);
}

protected function styler(): void
{
$application = new Application();
$application->run($this->getArgv());
}

protected function getArgv(): ArgvInput
{
return new ArgvInput(
$this->resolveOptions()
);
}

protected function resolveOptions(): array
{
return Arrayable::of($this->getOptions())
->map(static function (mixed $value, string $key) {
if (is_bool($value)) {
return $key;
}

return sprintf('%s=%s', $key, $value);
})
->values()
->get();
}

protected function getOptions(): array
{
return array_merge($this->options, $this->options_check);
}
}
Loading

0 comments on commit 12bc0b6

Please sign in to comment.