Skip to content

Commit

Permalink
Merge pull request #64 from acelaya-forks/feature/remove-functional
Browse files Browse the repository at this point in the history
Remove dependency on functional-php
  • Loading branch information
acelaya authored Nov 28, 2023
2 parents 647f3ff + 25054a7 commit 4616c54
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 33 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).

## [Unreleased]
### Added
* *Nothing*

### Changed
* Remove dependency on functional-php

### Deprecated
* *Nothing*

### Removed
* *Nothing*

### Fixed
* *Nothing*


## [5.2.0] - 2023-11-24
### Added
* Add support for PHP 8.3
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"ext-json": "*",
"laminas/laminas-servicemanager": "^3.22",
"league/csv": "^9.11",
"lstrojny/functional-php": "^1.17",
"shlinkio/shlink-config": "^2.4",
"shlinkio/shlink-json": "^1.0",
"symfony/console": "^6.3"
Expand Down Expand Up @@ -59,7 +58,7 @@
"test": "phpunit --order-by=random --testdox --colors=always",
"test:ci": "@test --coverage-clover=build/clover.xml --coverage-xml=build/coverage-xml --log-junit=build/junit.xml",
"test:pretty": "@test --coverage-html build/coverage-html",
"infect": "infection --threads=4 --min-msi=75 --log-verbosity=default --only-covered",
"infect": "infection --threads=4 --min-msi=80 --log-verbosity=default --only-covered",
"infect:ci": "@infect --coverage=build --skip-initial-tests",
"infect:test": [
"@test:ci",
Expand Down
4 changes: 2 additions & 2 deletions src/Params/ParamsUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Shlinkio\Shlink\Importer\Params;

use function Functional\map;
use function array_map;

final class ParamsUtils
{
Expand All @@ -14,6 +14,6 @@ final class ParamsUtils
*/
public static function invokeCallbacks(array $callbacksMap): array
{
return map($callbacksMap, static fn (callable $callback) => $callback());
return array_map(static fn (callable $callback) => $callback(), $callbacksMap);
}
}
10 changes: 5 additions & 5 deletions src/Sources/Bitly/BitlyApiImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use Shlinkio\Shlink\Importer\Util\DateHelper;
use Throwable;

use function Functional\filter;
use function Functional\map;
use function array_filter;
use function array_map;
use function is_array;
use function ltrim;
use function parse_url;
Expand Down Expand Up @@ -93,12 +93,12 @@ private function loadUrlsForGroup(
['links' => $links, 'pagination' => $pagination] = $this->callToBitlyApi($url, $params, $progressTracker);
$progressTracker->updateLastProcessedGroup($groupId);

$filteredLinks = filter(
$filteredLinks = array_filter(
$links,
static fn (array $link): bool => isset($link['long_url']) && ! empty($link['long_url']),
);

yield from map($filteredLinks, function (array $link) use ($params, $progressTracker): ImportedShlinkUrl {
yield from array_map(function (array $link) use ($params, $progressTracker): ImportedShlinkUrl {
$hasCreatedDate = isset($link['created_at']);
if ($hasCreatedDate) {
$progressTracker->updateLastProcessedUrlDate($link['created_at']);
Expand All @@ -116,7 +116,7 @@ private function loadUrlsForGroup(
$title = $link['title'] ?? null;

return new ImportedShlinkUrl(ImportSource::BITLY, $longUrl, $tags, $date, $domain, $shortCode, $title);
});
}, $filteredLinks);
} while (! empty($pagination['next']));
}

Expand Down
10 changes: 5 additions & 5 deletions src/Sources/Csv/CsvImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use function array_filter;
use function array_key_exists;
use function explode;
use function Functional\reduce_left;
use function parse_url;
use function str_contains;
use function str_replace;
Expand Down Expand Up @@ -68,12 +67,13 @@ private function importShortUrls(CsvParams $params): iterable

private function remapRecordHeaders(array $record): array
{
return reduce_left($record, static function ($value, string $index, array $c, array $acc) {
$normalized = [];
foreach ($record as $index => $value) {
$normalizedKey = strtolower(str_replace(' ', '', $index));
$acc[$normalizedKey] = $value;
$normalized[$normalizedKey] = $value;
}

return $acc;
}, []);
return $normalized;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Sources/ImportSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Shlinkio\Shlink\Importer\Params\ImportParams;

use function Functional\map;
use function array_map;

enum ImportSource: string
{
Expand All @@ -31,6 +31,6 @@ public function toParamsWithCallableMap(array $callableMap): ImportParams

public static function values(): array
{
return map(self::cases(), static fn (ImportSource $source) => $source->value);
return array_map(static fn (ImportSource $source) => $source->value, self::cases());
}
}
6 changes: 3 additions & 3 deletions src/Sources/Kutt/KuttImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Shlinkio\Shlink\Importer\Strategy\ImporterStrategyInterface;
use Throwable;

use function Functional\map;
use function array_map;
use function http_build_query;
use function sprintf;

Expand Down Expand Up @@ -80,7 +80,7 @@ public function loadUrls(KuttParams $params, int $skip = 0): iterable
*/
private function mapUrls(array $urls): array
{
return map($urls, function (array $url): ImportedShlinkUrl {
return array_map(function (array $url): ImportedShlinkUrl {
$visitsCount = $url['visit_count'];

return new ImportedShlinkUrl(
Expand All @@ -99,6 +99,6 @@ private function mapUrls(array $urls): array
null,
),
);
});
}, $urls);
}
}
14 changes: 7 additions & 7 deletions src/Sources/Shlink/ShlinkImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
use Shlinkio\Shlink\Importer\Strategy\ImporterStrategyInterface;
use Throwable;

use function array_map;
use function array_reverse;
use function ceil;
use function Functional\map;
use function http_build_query;
use function sprintf;

Expand Down Expand Up @@ -101,7 +101,7 @@ private function shouldContinue(array $pagination): bool
*/
private function mapUrls(array $urls, ShlinkParams $params): array
{
return map($urls, function (array $url) use ($params): ImportedShlinkUrl {
return array_map(function (array $url) use ($params): ImportedShlinkUrl {
// Shlink returns visits ordered from newer to older. To keep stats working once imported, we need to
// reverse them.
// In order to do that, we calculate the amount of pages we will get, and start from last to first.
Expand All @@ -116,7 +116,7 @@ private function mapUrls(array $urls, ShlinkParams $params): array
: [],
$this->importStartTime,
);
});
}, $urls);
}

/**
Expand All @@ -135,9 +135,9 @@ private function loadVisits(string $shortCode, ?string $domain, ShlinkParams $pa
['X-Api-Key' => $params->apiKey, 'Accept' => 'application/json'],
);

yield from array_reverse(map(
$parsedBody['visits']['data'] ?? [],
yield from array_reverse(array_map(
fn (array $visit) => $this->mapper->mapVisit($visit, $this->importStartTime),
$parsedBody['visits']['data'] ?? [],
));

if ($page > 1) {
Expand Down Expand Up @@ -189,9 +189,9 @@ private function loadOrphanVisits(ShlinkParams $params, int $page): iterable
['X-Api-Key' => $params->apiKey, 'Accept' => 'application/json'],
);

yield from array_reverse(map(
$parsedBody['visits']['data'] ?? [],
yield from array_reverse(array_map(
fn (array $visit) => $this->mapper->mapOrphanVisit($visit, $this->importStartTime),
$parsedBody['visits']['data'] ?? [],
));

if ($page > 1) {
Expand Down
10 changes: 5 additions & 5 deletions src/Sources/Yourls/YourlsImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Shlinkio\Shlink\Importer\Util\DateHelper;
use Throwable;

use function Functional\map;
use function array_map;
use function http_build_query;
use function sprintf;

Expand Down Expand Up @@ -64,7 +64,7 @@ private function loadUrls(YourlsParams $params): Generator
{
$result = $this->callYourlsApi(self::LINKS_ACTION, $params);

yield from map($result, function (array $url) use ($params) {
yield from array_map(function (array $url) use ($params) {
$shortCode = $url['keyword'] ?? '';

return new ImportedShlinkUrl(
Expand All @@ -78,14 +78,14 @@ private function loadUrls(YourlsParams $params): Generator
$params->importVisits ? $this->loadVisits($shortCode, $params) : [],
(int) ($url['clicks'] ?? 0),
);
});
}, $result);
}

private function loadVisits(string $shortCode, YourlsParams $params): Generator
{
$result = $this->callYourlsApi(self::VISITS_ACTION, $params, $shortCode);

yield from map($result, function (array $visit) {
yield from array_map(function (array $visit) {
$referer = $visit['referrer'] ?? '';

return new ImportedShlinkVisit(
Expand All @@ -94,7 +94,7 @@ private function loadVisits(string $shortCode, YourlsParams $params): Generator
DateHelper::nullableDateFromFormatWithDefault(self::YOURLS_DATE_FORMAT, $visit['click_time'] ?? null),
new ImportedShlinkVisitLocation($visit['country_code'], '', '', '', '', 0.0, 0.0),
);
});
}, $result);
}

private function callYourlsApi(string $action, YourlsParams $params, string $shortCode = ''): array
Expand Down
4 changes: 2 additions & 2 deletions test/Sources/Shlink/ShlinkImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Shlinkio\Shlink\Importer\Sources\Shlink\ShlinkMapper;

use function array_merge;
use function Functional\contains;
use function in_array;
use function sprintf;
use function str_contains;

Expand Down Expand Up @@ -145,7 +145,7 @@ function (string $url) use (&$urlsCallNum, $shortUrl, $visit1, $visit2): array {
foreach ($url->visits as $index => $visit) {
$visits[] = $visit;

self::assertEquals(contains([3, 4], $index) ? 'visit1' : 'visit2', $visit->referer);
self::assertEquals(in_array($index, [3, 4], true) ? 'visit1' : 'visit2', $visit->referer);
self::assertEquals(
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.10',
$visit->userAgent,
Expand Down

0 comments on commit 4616c54

Please sign in to comment.