Skip to content

Commit

Permalink
Add PHPStan, PHPUnit GitHub actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikus Rozenbergs committed Mar 7, 2024
1 parent 8511c53 commit 45eae1b
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 116 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: PHPStan

on: [push, pull_request]

jobs:
phpstan:
name: phpstan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
coverage: none

- name: Install composer dependencies
run: composer install -n --prefer-dist
env:
COMPOSER_AUTH: ${{ secrets.COMPOSER_AUTH }}

- name: Run PHPStan
run: ./vendor/bin/phpstan --error-format=github
18 changes: 18 additions & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: PHPUnit

on: [push, pull_request]

jobs:
build-test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: php-actions/composer@v6

- name: PHPUnit Tests
uses: php-actions/phpunit@master
with:
bootstrap: vendor/autoload.php
configuration: phpunit.xml
args: --coverage-text
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "^10.5",
"phpunit/php-code-coverage": "^10.1",
"phpunit/phpunit": "^9.5",
"phpunit/php-code-coverage": "^9.2",
"php-coveralls/php-coveralls": "^2.7",
"smalot/pdfparser": "^2.9",
"friendsofphp/php-cs-fixer": "^3.51",
"phpstan/phpstan": "^1.10"
},
"autoload": {
"psr-4": {
"mixisLv\\NameDays\\": "src"
},
"autoload-dev": {
"psr-4": {
"mixisLv\\NameDays\\Tests\\": "tests"
}
}
}
}
1 change: 1 addition & 0 deletions data/name-days-lv.json
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@
"Skaidra"
],
"02-29": [
""
],
"03-01": [
"Ivars",
Expand Down
6 changes: 4 additions & 2 deletions examples/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<html>
<html lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Latvian Name Days Examples</title>
Expand All @@ -10,7 +10,9 @@

<?php
$file = file_get_contents('./examples.php');
highlight_string($file);
if ($file) {
highlight_string($file);
}
?>

</body>
Expand Down
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
level: max
paths:
- src
- examples
- tests
- update
36 changes: 15 additions & 21 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<testsuite>
<directory suffix="Test.php">tests</directory>
</testsuite>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<logging>
<!-- and this is where your report will be written -->
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" beStrictAboutCoversAnnotation="true" beStrictAboutOutputDuringTests="true" beStrictAboutTodoAnnotatedTests="true" verbose="true">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<testsuite name="NameDays Test Suite">
<directory suffix="Test.php">tests</directory>
</testsuite>
<logging>
<!-- and this is where your report will be written -->
</logging>
</phpunit>
29 changes: 16 additions & 13 deletions src/NameDays.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

class NameDays
{
private $data = [];

/**
* @var array<string, array<int, string>> $data
*/
private array $data = [];

/**
* NameDays constructor.
Expand All @@ -18,11 +20,9 @@ class NameDays
*
* @throws \Exception
*/
public function __construct($source = 'name-days-lv')
public function __construct(string $source = 'name-days-lv')
{
$this->loadData($source);

return $this;
}

/**
Expand All @@ -32,10 +32,8 @@ public function __construct($source = 'name-days-lv')
*
* @throws \Exception
*/
private function loadData($source)
private function loadData(string $source): void
{
$dataFile = null;

switch ($source) {
case 'name-days-lv':
$dataFile = __DIR__ . '/../data/name-days-lv.json';
Expand All @@ -45,10 +43,15 @@ private function loadData($source)
break;
default:
throw new \Exception('NameDays data source not found');
break;
}

$this->data = json_decode(file_get_contents($dataFile), true);
$data = [];
$json = file_get_contents($dataFile);
if ($json) {
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
}

$this->data = is_array($data) ? $data : [];
}

/**
Expand All @@ -58,7 +61,7 @@ private function loadData($source)
*
* @return Names
*/
public function getNames($date = null)
public function getNames(string $date = null): Names
{
$key = substr($date ?? date('m-d'), -5);

Expand All @@ -73,12 +76,12 @@ public function getNames($date = null)
*
* @return string
*/
public function getDate($name, $withYear = false)
public function getDate(string $name, bool $withYear = false): ?string
{
$date = null;
$searchName = mb_strtolower($name);
foreach ($this->data as $key => $names) {
if (in_array($searchName, array_map('mb_strtolower', $names))) {
if (in_array($searchName, array_map('mb_strtolower', $names), true)) {
$date = (new Names($key, $names))->key();
break;
}
Expand Down
18 changes: 9 additions & 9 deletions src/Names.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
class Names
{
/** @var string */
private $key;
private string $key;

/** @var array */
private $names;
/** @var array<int, string> */
private array $names;

/**
* Names constructor.
*
* @param string $key
* @param array $names
* @param array<int, string> $names
*/
public function __construct(string $key, array $names)
{
Expand All @@ -31,7 +31,7 @@ public function __construct(string $key, array $names)
*
* @return string
*/
public function key()
public function key(): string
{
return $this->key;
}
Expand All @@ -41,17 +41,17 @@ public function key()
*
* @return string
*/
public function toString()
public function toString(): string
{
return join(", ", $this->names);
return implode(", ", $this->names);
}

/**
* toArray
*
* @return array
* @return array<int, string>
*/
public function toArray()
public function toArray(): array
{
return $this->names;
}
Expand Down
86 changes: 18 additions & 68 deletions update/lv.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!doctype html>
<html>
<html lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Latvian Name Days Examples DB update</title>
<title>Latvian Name Days DB update</title>
</head>
<body>

Expand All @@ -11,80 +11,30 @@

require_once __DIR__ . '/../vendor/autoload.php'; // Autoload files using Composer autoload

function getJsonString($url)
function getJsonString(string $url): bool|string
{
// Parse PDF file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile($url);
$nameDays = parseNames(explode("\n", $pdf->getText()));
$data = file_get_contents($url);

$nameDays = parseNames(explode("\n", $data));

Check failure on line 18 in update/lv.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #2 $string of function explode expects string, string|false given.

return json_encode($nameDays, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}

function parseNames($rows)
/**
* @param array<int, string> $rows
* @return array<string, array<int, string>>
*/
function parseNames(array $rows): array
{
$month = 0;
$nameDays = [];
foreach ($rows as $row) {
$data = explode(".", $row);
if (isset($data[1]) && (int)$data[0] > 0) {
$nameDays[sprintf("%'.02d", $month) . "-" . sprintf("%'.02d", $data[0])] = array_map(
$data = explode(";", $row);
$date = explode(".", $data[0]);
if (isset($date[0],$date[1])) {
$nameDays[sprintf("%'.02d", $date[1]) . "-" . sprintf("%'.02d", $date[0])] = array_map(
'trim',
explode(',', str_replace(" ", "", $data[1]))
explode(' ', $data[1])
);
} else {
if (strlen(trim($data[0])) < 4 || strlen(trim($data[0])) > 40) { // kaut kas nav labi ar mēnešiem
continue;
}
$month++;
}
}

return $nameDays;
}


function getJsonStringExt($url)
{
// Parse PDF file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile($url);
$nameDays = parseNamesExt(explode("\n", $pdf->getText()));

return json_encode($nameDays, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}

function parseNamesExt($rows)
{
$month = 0;
$date = 0;

$nameDays = [];

array_shift($rows);
array_shift($rows);

foreach ($rows as $row) {
$data = explode(".", $row);
$value = str_replace([" ", "\t"], "", $data[0]);

//print_r([$value]);
if (in_array(trim($value), ['JANVĀRIS', 'FEBRUĀRIS', 'MARTS', 'APRĪLIS', 'MAIJS', 'JŪNIJS', 'JŪLIJS', 'AUGUSTS', 'SEPTEMBRIS', 'OKTOBRIS', 'NOVEMBRIS', 'DECEMBRIS'])) {
$month++;
//print_r([$month, $value]);
} else {
if ((int)$value > 0) {
$date = (int)$value;
} else {
$key = sprintf("%'.02d", $month) . "-" . sprintf("%'.02d", $date);
$values = array_filter(array_map('trim', explode(',', $value)), 'strlen');

if (isset($nameDays[$key])) {
$nameDays[$key] = array_merge($nameDays[$key], $values);
} else {
$nameDays[$key] = $values;
}
}
}
}

Expand All @@ -95,10 +45,10 @@ function parseNamesExt($rows)

echo $lastUpdate;

$jsonLvStd = getJsonString('http://vvc.gov.lv/advantagecms/export/docs/komisijas/tradic_v%C4%81rdadienu_saraksts_2022.pdf');
$jsonLvStd = getJsonString('https://www.vvc.gov.lv/lv/media/157/download');
file_put_contents('./../data/name-days-lv.json', $jsonLvStd);

$jsonLvExtended = getJsonStringExt('http://vvc.gov.lv/advantagecms/export/docs/komisijas/paplasinatais_saraksts.pdf');
$jsonLvExtended = getJsonString('https://www.vvc.gov.lv/lv/media/156/download');
file_put_contents('./../data/name-days-lv-extended.json', $jsonLvExtended);

?>
Expand Down

0 comments on commit 45eae1b

Please sign in to comment.