Skip to content

Commit

Permalink
Add cs phpstan github-actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Galek committed Aug 3, 2023
1 parent 7d6750e commit 9fba165
Show file tree
Hide file tree
Showing 22 changed files with 321 additions and 60 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/codesniffer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: "Codesniffer"

on:
pull_request:

push:
branches: [ "*" ]

schedule:
- cron: "0 8 * * 1"

jobs:
phpstan:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: "Setup PHP"
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress
- name: Run coding standards
run: make cs
39 changes: 39 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: PHPstan

on:
pull_request:

push:
branches: [ "*" ]

schedule:
- cron: "0 8 * * 1"

jobs:
phpstan:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: "Setup PHP"
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress
- name: Run phpstan
run: make phpstan
39 changes: 39 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: PHP Unit tests

on:
pull_request:

push:
branches: [ "*" ]

schedule:
- cron: "0 8 * * 1"

jobs:
test82:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: "Setup PHP"
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress
- name: Run tests
run: make tests
17 changes: 16 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
.PHONY: install tests
.PHONY: install tests qa cs csf phpstan

install:
composer install

qa: phpstan cs

cs:
ifdef GITHUB_ACTION
vendor/bin/phpcs --standard=ruleset.xml --encoding=utf-8 --colors -nsp -q --report=checkstyle src tests
else
vendor/bin/phpcs --standard=ruleset.xml --encoding=utf-8 --colors -nsp src tests
endif

csf:
vendor/bin/phpcbf --standard=ruleset.xml --encoding=utf-8 --colors -nsp src tests

phpstan:
vendor/bin/phpstan analyse -c phpstan.neon

tests:
vendor/bin/phpunit --bootstrap vendor/autoload.php tests
15 changes: 14 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
"name": "troia-studio/ssh-key-validator",
"type": "library",
"license": "MIT",
"description": "SSH Public Key validator",
"autoload": {
"psr-4": {
"TroiaStudio\\SshKeyValidator\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
}
},
"authors": [
{
"name": "Jan Galek",
Expand All @@ -18,6 +24,13 @@
"php": ">=8.2"
},
"require-dev": {
"phpunit/phpunit": "^10"
"phpunit/phpunit": "^10",
"contributte/qa": "^0.3.1",
"contributte/phpstan": "^0.1.0"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
15 changes: 15 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
includes:
- vendor/contributte/phpstan/phpstan.neon

parameters:
level: 0
phpVersion: 80200

scanDirectories:
- src

fileExtensions:
- php

paths:
- src
28 changes: 28 additions & 0 deletions ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="TroiaStudio" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
<!-- Rulesets -->
<rule ref="./vendor/contributte/qa/ruleset-8.2.xml">
<exclude name="Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed" />
</rule>
<config name="php_version" value="80200"/>

<!-- Rules -->
<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
<properties>
<property name="rootNamespaces" type="array">
<element key="src" value="TroiaStudio\SshKeyValidator"/>
<element key="tests" value="Tests"/>
</property>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<properties>
<property name="declareOnFirstLine" value="0"/>
</properties>
</rule>

<rule ref="Generic.WhiteSpace.DisallowTabIndent" />

<!-- Excludes -->
<exclude-pattern>/tests/tmp</exclude-pattern>
</ruleset>
7 changes: 5 additions & 2 deletions src/Enums/SSH_KEY.php → src/Enums/SshKey.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?php
declare(strict_types=1);

declare(strict_types = 1);

namespace TroiaStudio\SshKeyValidator\Enums;

enum SSH_KEY: string
enum SshKey: string
{

case DSS = '/^(ssh-dss) (AAAAB3NzaC1kc3[0-9A-Za-z+\/]+[=]{0,3})(\s.*)?$/';
case ECDSA_SHA2_NITSTP256 = '/^(ecdsa-sha2-nistp256) (AAAAE2VjZHNhLXNoYTItbmlzdHAyNT[0-9A-Za-z+\/]+[=]{0,3})(\s.*)?$/';
case SK_ECDSA_SHA2_NITSTP256 = '/^([email protected]) (AAAAInNrLWVjZHNhLXNoYTItbmlzdHAyNTZAb3BlbnNzaC5jb2[0-9A-Za-z+\/]+[=]{0,3})(\s.*)?$/';
case ED25519 = '/^(ssh-ed25519) (AAAAC3NzaC1lZDI1NTE5[0-9A-Za-z+\/]+[=]{0,3})(\s.*)?$/';
case SK_ED25519 = '/^([email protected]) (AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29t[0-9A-Za-z+\/]+[=]{0,3})(\s.*)?$/';
case RSA = '/^(ssh-rsa) (AAAAB3NzaC1yc2[0-9A-Za-z+\/]+[=]{0,3})(\s.*)?$/';

}
13 changes: 8 additions & 5 deletions src/Key.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php
declare(strict_types=1);

declare(strict_types = 1);

namespace TroiaStudio\SshKeyValidator;

use TroiaStudio\SshKeyValidator\Enums\SSH_KEY;
use TroiaStudio\SshKeyValidator\Enums\SshKey;

class Key
{
protected SSH_KEY $key;

protected SshKey $key;

protected string $type;

Expand All @@ -17,7 +19,7 @@ class Key

protected string $comment = '';

public function __construct(SSH_KEY $key, string $value, string $prefix, string $comment = '')
public function __construct(SshKey $key, string $value, string $prefix, string $comment = '')
{
$this->key = $key;
$this->type = $key->name;
Expand Down Expand Up @@ -45,4 +47,5 @@ public function getComment(): string
{
return $this->comment;
}
}

}
14 changes: 8 additions & 6 deletions src/KeyFactory.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
declare(strict_types=1);

declare(strict_types = 1);

namespace TroiaStudio\SshKeyValidator;

use TroiaStudio\SshKeyValidator\Enums\SSH_KEY;
use TroiaStudio\SshKeyValidator\Enums\SshKey;
use TroiaStudio\SshKeyValidator\Validators\DSSValidator;
use TroiaStudio\SshKeyValidator\Validators\EcdsaValidator;
use TroiaStudio\SshKeyValidator\Validators\Ed25519Validator;
Expand All @@ -14,12 +15,13 @@

class KeyFactory
{

/**
* @param Validator[] $validators
*/
public static function create(string $key, array $validators = []): ?Key
{
if (empty($validators)) {
if (count($validators) === 0) {
$validators = [
new DSSValidator(),
new EcdsaValidator(),
Expand All @@ -45,9 +47,9 @@ public static function create(string $key, array $validators = []): ?Key
* @internal
* @param array<int, array<int, string>> $data
*/
public static function createFromValidation(array $data, SSH_KEY $type): Key
public static function createFromValidation(array $data, SshKey $type): Key
{
return new Key($type, $data[2][0], $data[1][0], empty($data[3]) ? '' : trim($data[3][0]));
return new Key($type, $data[2][0], $data[1][0], trim($data[3][0]));
}

}
}
9 changes: 6 additions & 3 deletions src/KeyValidator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types=1);

declare(strict_types = 1);

namespace TroiaStudio\SshKeyValidator;

Expand All @@ -11,8 +12,9 @@
use TroiaStudio\SshKeyValidator\Validators\SKEd25519Validator;
use TroiaStudio\SshKeyValidator\Validators\Validator;

class KeyValidator
final class KeyValidator
{

/** @var Validator[] */
private array $validators;

Expand Down Expand Up @@ -52,4 +54,5 @@ public function validate(string $key): bool

return false;
}
}

}
5 changes: 3 additions & 2 deletions src/ValidationException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types=1);

declare(strict_types = 1);

namespace TroiaStudio\SshKeyValidator;

Expand All @@ -8,4 +9,4 @@
class ValidationException extends Exception
{

}
}
Loading

0 comments on commit 9fba165

Please sign in to comment.