Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: pr

on:
workflow_dispatch:
pull_request:
branches:
- master
types:
- opened
- reopened
- synchronize
- ready_for_review

jobs:
phpstan-phpunit:
name: PHP Tests
strategy:
matrix:
php: [ '7.4', '8.3', '8.5' ]
runs-on: ubuntu-24.04
steps:
- name: Git Checkout
uses: actions/checkout@v3
- run: echo "The ${{ github.repository }} repository has been cloned to the runner."

- name: Composer install
uses: php-actions/composer@v6
with:
php_version: ${{matrix.php}}
- run: echo "Composer dependencies have been installed"

- name: Code Scanning
id: code-scanning
uses: php-actions/phpstan@v3
with:
php_version: ${{matrix.php}}
path: src/
configuration: "phpstan.neon"
memory_limit: "768M"
- run: echo "Stan tests complete"

- name: Unit Tests
id: unit-tests
uses: php-actions/phpunit@v3
env:
CREATE_SNAPSHOTS: false
with:
version: 9.6.29
php_version: ${{matrix.php}}
configuration: "phpunit.xml"
memory_limit: "768M"

- run: echo "Unit tests complete"

outputs:
code-scanning-status: ${{ steps.code-scanning.outcome }}
unit-tests-status: ${{ steps.unit-tests.outcome }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/vendor
/composer.lock
.phpunit.cache/
.phpunit.result.cache
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
}
],
"require": {
"php": ">=7.4",
"php": "^7.4|^8.0",
"packaged/helpers": "~1.0||^2.0",
"ext-ctype": "*"
},
"require-dev": {
"ext-gettext": "*",
"phpunit/phpunit": "9.5.*"
"phpunit/phpunit": "^9.6",
"phpstan/phpstan": "^2.1"
},
"suggest": {
"ext-gettext": "*"
Expand Down
11 changes: 11 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
parameters:
level: 7
paths:
- src
ignoreErrors:
-
identifiers:
- trait.unused
- "#^Unsafe usage of new static#"
- '#^Method [a-zA-Z0-9\\$\\_\\\\:\\(\\)]+ has parameter [a-zA-Z0-9\\$\\_\\\\:\\(\\)]+ with no value type specified in iterable type array\.$#'
- '#^Method [a-zA-Z0-9\\$\\_\\\\:\\(\\)]+ has parameter [a-zA-Z0-9\\$\\_\\\\:\\(\\)]+ with no type specified\.$#'
37 changes: 20 additions & 17 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
timeoutForSmallTests="5"
timeoutForMediumTests="10"
timeoutForLargeTests="15"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
timeoutForSmallTests="5"
timeoutForMediumTests="10"
timeoutForLargeTests="15"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="tests">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
5 changes: 5 additions & 0 deletions src/Catalog/ArrayCatalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

class ArrayCatalog implements MessageCatalog
{
/**
* @var array<string, string|array<string, string>>
*/
protected array $_data;

/**
Expand All @@ -28,8 +31,10 @@ public function getMessage($messageId): ?Message
{
$this->_data[$messageId] = [Message::DEFAULT_OPTION => $this->_data[$messageId]];
}

return new Message($this->_data[$messageId]);
}

return null;
}
}
16 changes: 12 additions & 4 deletions src/Catalog/DynamicArrayCatalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@
class DynamicArrayCatalog extends ArrayCatalog
{
/**
* @var callable
* @var ?callable
*/
protected $_translateCallback;
protected $_translateCallback = null;

/**
* @var array<string, array<string, string>>
*/
protected array $_data;

/**
* @return array<string, array<string, string>>
*/
public function getData(): array
{
return $this->_data;
}

public function addMessage(string $messageId, array $options = [])
public function addMessage(string $messageId, array $options = []): self
{
$this->_data[$messageId] = $options;
return $this;
Expand All @@ -26,7 +34,7 @@ public function asPhpFile(bool $withNewLines = false): string

$content = ['<?php', PHP_EOL, 'return ['];

$callback = $this->_translateCallback;
$callback = $this->_translateCallback ?? null;

foreach($this->getData() as $mid => $options)
{
Expand Down
32 changes: 19 additions & 13 deletions src/Catalog/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ class Message
const DEFAULT_OPTION = '_';
const META_OPTION = '__meta';

/** @var array<string, string|array<string>> */
protected array $_options;
/**
* @var array
* @var string|int|null
*/
protected array $_options;

protected $_choice;
protected $_choiceNumeric;
protected $_choice = null;
protected ?bool $_choiceNumeric = null;

public function __construct(array $options = [])
{
$this->_options = $options;
}

public static function fromDefault($default)
public static function fromDefault($default): self
{
if(is_array($default))
{
Expand All @@ -29,7 +29,8 @@ public static function fromDefault($default)
if(is_scalar($default))
{
$matches = [];
preg_match_all('/((\[[^\]]+\])?([^\|]+))+/m', $default, $matches);
preg_match_all('/((\[[^\]]+\])?([^\|]+))+/m', (string)$default, $matches);
/** @phpstan-ignore isset.offset */
if(isset($matches[3]))
{
foreach($matches[2] as $i => $key)
Expand All @@ -42,9 +43,11 @@ public static function fromDefault($default)
return $msg;
}

/** @return string[] */
public function getMeta(): array
{
return $this->_options[self::META_OPTION] ?? [];
return isset($this->_options[self::META_OPTION]) && is_array($this->_options[self::META_OPTION])
? $this->_options[self::META_OPTION] : [];
}

public function addOption($key, $value): self
Expand All @@ -53,12 +56,13 @@ public function addOption($key, $value): self
return $this;
}

/** @return array<string, string|array<string>> */
public function getOptions(): array
{
return $this->_options;
}

protected function _setChoice($choice = null)
protected function _setChoice($choice = null): void
{
if($choice === null)
{
Expand Down Expand Up @@ -86,17 +90,18 @@ protected function _selectChoice(): string
if($key === self::DEFAULT_OPTION)
{
$return = $v;
if($this->_choice === null)
if($this->_choice === null && is_string($return))
{
return $return;
}
}
else if($this->_choiceMatches($key))
{
return $v;
return is_string($v) ? $v : '';
}
}
return $return;

return is_string($return) ? $return : '';
}

protected function _choiceMatches($optionKey): bool
Expand All @@ -111,10 +116,11 @@ protected function _choiceMatches($optionKey): bool
{
if($this->_choiceNumeric)
{
if($key === 'n' || ((is_int($key) || ctype_digit($key)) && (int)$key === $this->_choice))
if($key === 'n' || ((ctype_digit($key)) && (int)$key === $this->_choice))
{
return true;
}

if(strpos($key, '..') > 0)
{
[$min, $max] = explode('..', $key);
Expand Down
Loading