Skip to content

Commit

Permalink
Update tools and setup PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrech committed Apr 29, 2024
1 parent 857cbd3 commit 8e2cbe5
Show file tree
Hide file tree
Showing 34 changed files with 2,680 additions and 319 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ jobs:
with:
args: --config=.php-cs-fixer.php --diff --dry-run

phpstan:
name: Static Analysis
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: PHPStan
uses: docker://oskarstark/phpstan-ga
env:
REQUIRE_DEV: true

ci:
name: Test PHP ${{ matrix.php-version }} ${{ matrix.name }}
runs-on: ubuntu-latest
Expand Down Expand Up @@ -61,3 +73,21 @@ jobs:
- name: Run Tests
run: php vendor/bin/simple-phpunit

phar:
name: Create a PHAR and ensure it is working
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Build the PHAR
uses: ./.github/actions/phar

- name: Execute the PHAR
run: |
tools/phar/build/jolinotif.phar --help
- name: Trigger a notification
run: |
tools/phar/build/jolinotif.phar --title "Yolo" --body "Hello world!"
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@
/build/
/.php-cs-fixer.cache
/.phpunit.result.cache
/var/

# Castor
.castor.stub.php
2 changes: 2 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
->in(__DIR__)
->append([
__FILE__,
'castor.php',
])
->notPath('var')
;

return (new PhpCsFixer\Config())
Expand Down
25 changes: 21 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ Here's some tips to make you the best contributor ever:
* [Standard code](#standard-code)
* [Keeping your fork up-to-date](#keeping-your-fork-up-to-date)

Also, you will need to install Castor to run the tests and fix CS violations.
See [Castor's documentation](https://castor.jolicode.com/getting-started/installation/)
for more information.

To install all the dependencies and tools, run the following command:

```shell
castor install
```

## Rules

Here are a few rules to follow in order to ease code reviews, and discussions
Expand Down Expand Up @@ -48,16 +58,23 @@ changes, improvements or alternatives may be given).
Run the tests using the following script:

```shell
make test
castor phpunit
```

## Standard code

Use [PHP CS fixer](http://cs.sensiolabs.org/) to make your code compliant with
JoliNotif's coding standards:
Use PHP-CS-Fixer to make your code compliant with JoliNotif's coding standards:

```shell
castor cs
```

## Static analysis

Use PHPStan to ensure the code is free of errors:

```shell
make cs
castor phpstan
```

## Keeping your fork up-to-date
Expand Down
15 changes: 0 additions & 15 deletions Makefile

This file was deleted.

36 changes: 36 additions & 0 deletions castor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the JoliNotif project.
*
* (c) Loïck Piera <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Castor\Attribute\AsRawTokens;
use Castor\Attribute\AsTask;

use function Castor\import;
use function Castor\mount;
use function Castor\run;

import(__DIR__ . '/tools/php-cs-fixer/castor.php');
import(__DIR__ . '/tools/phpstan/castor.php');

mount(__DIR__ . '/tools/phar');

#[AsTask(description: 'Install dependencies')]
function install(): void
{
run(['composer', 'install'], workingDirectory: __DIR__);
qa\cs\install();
qa\phpstan\install();
}

#[AsTask(description: 'Run PHPUnit', ignoreValidationErrors: true)]
function phpunit(#[AsRawTokens] array $rawTokens): void
{
run(['vendor/bin/simple-phpunit', ...$rawTokens]);
}
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"symfony/process": "^5.4 || ^6.0 || ^7.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.13",
"symfony/finder": "^5.4 || ^6.0 || ^7.0",
"symfony/phpunit-bridge": "^5.4 || ^6.0 || ^7.0"
},
Expand Down
12 changes: 12 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
includes:
#- phpstan-baseline.neon

parameters:
level: 9
paths:
- src
tmpDir: 'var/phpstan/tmp'
inferPrivatePropertyTypeFromConstructor: true
checkGenericClassInNonGenericObjectType: false
excludePaths:
analyse: []
6 changes: 3 additions & 3 deletions src/Exception/InvalidNotificationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class InvalidNotificationException extends \LogicException implements Exception

public function __construct(
Notification $notification,
$message = '',
$code = 0,
?Exception $previous = null
string $message = '',
int $code = 0,
?\Throwable $previous = null
) {
$this->notification = $notification;

Expand Down
6 changes: 3 additions & 3 deletions src/Exception/NoSupportedNotifierException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
class NoSupportedNotifierException extends \RuntimeException implements Exception
{
public function __construct(
$message = 'No supported notifier',
$code = 0,
?Exception $previous = null
string $message = 'No supported notifier',
int $code = 0,
?\Throwable $previous = null
) {
parent::__construct($message, $code, $previous);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Notification
private ?string $title = null;
private ?string $body = null;
private ?string $icon = null;
/** @var array<string, string|int> */
private array $options = [];

public function getTitle(): ?string
Expand Down Expand Up @@ -62,17 +63,17 @@ public function setIcon(string $icon): self
: realpath($icon);
}

$this->icon = $icon;
$this->icon = \is_string($icon) ? $icon : null;

return $this;
}

public function getOption(string $key): ?string
public function getOption(string $key): string|int|null
{
return $this->options[$key] ?? null;
}

public function addOption(string $key, string $option): self
public function addOption(string $key, string|int $option): self
{
$this->options[$key] = $option;

Expand Down
6 changes: 3 additions & 3 deletions src/Notifier/AppleScriptNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ public function getPriority(): int

protected function getCommandLineArguments(Notification $notification): array
{
$script = 'display notification "' . str_replace('"', '\\"', $notification->getBody()) . '"';
$script = 'display notification "' . str_replace('"', '\\"', $notification->getBody() ?? '') . '"';

if ($notification->getTitle()) {
$script .= ' with title "' . str_replace('"', '\\"', $notification->getTitle()) . '"';
}

if ($notification->getOption('subtitle')) {
$script .= ' subtitle "' . str_replace('"', '\\"', $notification->getOption('subtitle')) . '"';
$script .= ' subtitle "' . str_replace('"', '\\"', (string) $notification->getOption('subtitle')) . '"';
}

if ($notification->getOption('sound')) {
$script .= ' sound name "' . str_replace('"', '\\"', $notification->getOption('sound')) . '"';
$script .= ' sound name "' . str_replace('"', '\\"', (string) $notification->getOption('sound')) . '"';
}

return [
Expand Down
2 changes: 2 additions & 0 deletions src/Notifier/BinaryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function getEmbeddedBinary(): string;
* used inside a phar archive.
*
* All paths should be relative to the directory pointed by getRootDir().
*
* @return list<string>
*/
public function getExtraFiles(): array;
}
2 changes: 2 additions & 0 deletions src/Notifier/CliBasedNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public function send(Notification $notification): bool

/**
* Configure the process to run in order to send the notification.
*
* @return list<string|int>
*/
abstract protected function getCommandLineArguments(Notification $notification): array;

Expand Down
2 changes: 1 addition & 1 deletion src/Notifier/GrowlNotifyNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function getCommandLineArguments(Notification $notification): array
{
$arguments = [
'--message',
$notification->getBody(),
$notification->getBody() ?? '',
];

if ($notification->getTitle()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Notifier/KDialogNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function getCommandLineArguments(Notification $notification): array
}

$arguments[] = '--passivepopup';
$arguments[] = $notification->getBody();
$arguments[] = $notification->getBody() ?? '';

// Timeout, in seconds
$arguments[] = 5;
Expand Down
2 changes: 1 addition & 1 deletion src/Notifier/NotifuNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function getCommandLineArguments(Notification $notification): array
{
$arguments = [
'/m',
$notification->getBody(),
$notification->getBody() ?? '',
];

if ($notification->getTitle()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Notifier/NotifySendNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function getCommandLineArguments(Notification $notification): array
$arguments[] = $notification->getTitle();
}

$arguments[] = $notification->getBody();
$arguments[] = $notification->getBody() ?? '';

return $arguments;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Notifier/SnoreToastNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected function getCommandLineArguments(Notification $notification): array
{
$arguments = [
'-m',
$notification->getBody(),
$notification->getBody() ?? '',
];

if ($notification->getTitle()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Notifier/TerminalNotifierNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function getCommandLineArguments(Notification $notification): array
{
$arguments = [
'-message',
$notification->getBody(),
$notification->getBody() ?? '',
];

if ($notification->getTitle()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Notifier/ToasterNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function getCommandLineArguments(Notification $notification): array
{
$arguments = [
'-m',
$notification->getBody(),
$notification->getBody() ?? '',
];

if ($notification->getTitle()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Notifier/WslNotifySendNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function getCommandLineArguments(Notification $notification): array
$arguments = [
'--appId',
'JoliNotif',
$notification->getBody(),
$notification->getBody() ?? '',
];

if ($notification->getTitle()) {
Expand Down
52 changes: 29 additions & 23 deletions tools/phar/box.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
{
"base-path": "../../",
"directories": [
"bin",
"src"
],
"files": [
"LICENSE",
"vendor/autoload.php"
],
"finder": [
{
"name": "*.php",
"in": "vendor/composer"
},
{
"name": "*.php",
"exclude": ["Tests"],
"in": "vendor/symfony/process"
}
],
"git-version": "package_version",
"main": "jolinotif",
"output": "tools/phar/build/jolinotif.phar"
"base-path": "../../",
"directories": [
"bin",
"src"
],
"files": [
"LICENSE",
"vendor/autoload.php"
],
"finder": [
{
"name": "*.php",
"in": "vendor/composer"
},
{
"name": "*.php",
"exclude": ["Tests"],
"in": "vendor/symfony/process"
}
],
"compactors": [
"KevinGH\\Box\\Compactor\\Php"
],
"annotations": false,
"compression": "GZ",
"check-requirements": false,
"git-version": "package_version",
"main": "jolinotif",
"output": "tools/phar/build/jolinotif.phar"
}
Loading

0 comments on commit 8e2cbe5

Please sign in to comment.