Skip to content

Commit

Permalink
Add CI/CD and phpstan-related fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zozlak committed Nov 22, 2024
1 parent 6444e25 commit 4fe9db6
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 11 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: test and deploy
on:
workflow_dispatch: ~
push: ~
release:
types: [published]
jobs:
phpunit:
strategy:
matrix:
php: [8.1, 8.2, 8.3, 8.4]
imageTag: ['latest']
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: json,mbstring,pcre,yaml
coverage: xdebug
env:
fail-fast: true
- name: composer update
run: |
composer update
- name: phpstan
run: |
vendor/bin/phpstan analyze -l 6 src tests
- name: phpunit
run: |
XDEBUG_MODE=coverage vendor/bin/phpunit
- name: coveralls
run: |
export COVERALLS_RUN_LOCALLY=1
export COVERALLS_REPO_TOKEN=${{ secrets.coverallsToken }}
rm -fR composer.json composer.lock vendor
composer require php-coveralls/php-coveralls
php vendor/bin/php-coveralls -v
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/config.yaml
/*.ttl*
/.phpunit.cache
/build
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# arche-ref-sources

[![Build status](https://github.com/acdh-oeaw/arche-ref-sources/actions/workflows/test.yaml/badge.svg)](https://github.com/acdh-oeaw/arche-ref-sources/actions/workflows/test.yaml)
[![Coverage Status](https://coveralls.io/repos/github/acdh-oeaw/arche-ref-sources/badge.svg?branch=master)](https://coveralls.io/github/acdh-oeaw/arche-ref-sources?branch=master)

A script for enhancing an ARCHE Suite repository data with information gathered from external reference sources (Geonames, GND, wikidata, etc.).

It works by resolving existing identifiers that are external reference source URIs, fetching information from there and updating a corresponding ARCHE Suite repository resource.
Expand Down
6 changes: 5 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" cacheDirectory=".phpunit.cache">
<coverage/>
<coverage>
<report>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<testsuite name="tests">
<directory>tests</directory>
</testsuite>
Expand Down
12 changes: 9 additions & 3 deletions src/acdhOeaw/arche/refSources/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@

use Generator;
use Psr\Log\LoggerInterface;
use rdfInterface\DatasetNode;
use rdfInterface\DatasetInterface;
use rdfInterface\DatasetNodeInterface;
use acdhOeaw\UriNormalizer;
use acdhOeaw\UriNormalizerCache;
use acdhOeaw\UriNormalizerException;
use acdhOeaw\arche\lib\Schema;

/**
Expand All @@ -40,6 +42,7 @@
*/
class Crawler {

/** @phpstan-ignore property.onlyWritten */
private Schema $schema;
private PropertyMappings $mappings;
private UriNormalizer $normalizer;
Expand All @@ -57,8 +60,7 @@ public function __construct(object $refSrcsCfg, Schema $schema,
/**
*
* @param NamedEntityIteratorInterface $source
* @param bool $verbose
* @return Generator<array<DatasetNode>>
* @return Generator<array<DatasetInterface>>
*/
public function crawl(NamedEntityIteratorInterface $source,
string $dateFilter = '',
Expand Down Expand Up @@ -90,6 +92,10 @@ public function crawl(NamedEntityIteratorInterface $source,
}
}

/**
*
* @return array<DatasetInterface>
*/
private function processEntity(NamedEntityInterface $entity): array {
// collect data from all external databases reachable from this entity
$entityExtMeta = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ interface NamedEntityIteratorInterface {
public function setFilter(?string $class = null, ?string $idMatch = null,
?string $minModDate = null, ?int $limit = null): void;

/**
*
* @return Generator<NamedEntityInterface>
*/
public function getNamedEntities(): Generator;

public function getCount(): int;
Expand Down
4 changes: 2 additions & 2 deletions src/acdhOeaw/arche/refSources/NamedEntityRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use termTemplates\PredicateTemplate as PT;
use acdhOeaw\UriNormalizer;
use acdhOeaw\UriNormalizerException;
use acdhOeaw\arche\lib\Repo;
use acdhOeaw\arche\lib\RepoInterface;
use acdhOeaw\arche\lib\RepoResource;
use rdfInterface\DatasetNodeInterface;

Expand All @@ -43,7 +43,7 @@ class NamedEntityRepo implements NamedEntityInterface {
use NamedEntityTrait;

private RepoResource $res;
private Repo $repo;
private RepoInterface $repo;

public function __construct(RepoResource $res) {
$this->res = $res;
Expand Down
13 changes: 8 additions & 5 deletions src/acdhOeaw/arche/refSources/PropertyMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function __construct(object $cfg) {
$this->maxPerLang = $cfg->maxPerLang ?? PHP_INT_MAX;
$this->preferredLangs = $cfg->preferredLangs ?? [];
if (!empty($cfg->value)) {
/** @phpstan-ignore property.notFound */
$this->value = $cfg->type === 'resource' ? DF::namedNode($cfg->value) : DF::literal($cfg->value);
} else {
$this->path = array_map(fn($x) => DF::namedNode($x), $cfg->path);
Expand Down Expand Up @@ -213,13 +214,16 @@ private function processLang(DatasetInterface $meta): void {
// assure all values are literals
$meta->forEach(fn(QuadInterface $q) => $q->getObject() instanceof LiteralInterface ? $q : $q->withObject(DF::literal($q->getObject()->getValue())));
if (count($this->preferredLangs) > 0) {
/** @phpstan-ignore method.notFound */
$meta->deleteExcept(fn(QuadInterface $q) => in_array($q->getObject()->getLang(), $this->preferredLangs));
}
// sort according to language preferences if needed
$triples = iterator_to_array($meta->getIterator());
if ($this->maxPerLang < PHP_INT_MAX) {
uasort($triples, function ($a, $b) {
/** @phpstan-ignore method.notFound */
$aPref = array_search($a->getObject()->getLang(), $this->preferredLangs) ?: PHP_INT_MAX;
/** @phpstan-ignore method.notFound */
$bPref = array_search($b->getObject()->getLang(), $this->preferredLangs) ?: PHP_INT_MAX;
$comp = $aPref <=> $bPref;
if ($comp === 0) {
Expand All @@ -235,15 +239,18 @@ private function processLang(DatasetInterface $meta): void {
$literal = $triple->getObject();
/* @var $literal LiteralInterface */
$lang = match ($this->langProcess) {
/** @phpstan-ignore method.notFound */
PropertyMapping::LANG_PASS => $literal->getLang(),
/** @phpstan-ignore method.notFound */
PropertyMapping::LANG_ASSURE => $literal->getLang() ?? $this->langValue,
PropertyMapping::LANG_OVERWRITE => $this->langValue,
PropertyMapping::LANG_REMOVE => null,
default => throw new RuntimeException("Unsupported lang tag processing mode $process"),
default => throw new RuntimeException("Unsupported lang tag processing mode $this->langProcess"),
};
$lang ??= '';
$counts[$lang] = ($counts[$lang] ?? 0) + 1;
if ($counts[$lang] <= $this->maxPerLang) {
/** @phpstan-ignore method.notFound */
$toAdd = $literal->withLang($lang);
if (!empty($this->datatype)) {
$toAdd = $toAdd->withDatatype($this->datatype);
Expand Down Expand Up @@ -275,11 +282,7 @@ private function normalize(DatasetInterface $meta, UriNormalizer $normalizer): v

/**
*
* @param DatasetInterface $meta
* @param NamedNodeInterface $sbj
* @param UriNormalizer $normalizer
* @param array<NamedNodeInterface> $path
* @return DatasetInterface
*/
private function resolveRecursively(DatasetInterface $meta,
TermInterface $sbj,
Expand Down

0 comments on commit 4fe9db6

Please sign in to comment.