Skip to content

Commit

Permalink
Round 2 of rector
Browse files Browse the repository at this point in the history
  • Loading branch information
spekulatius committed Aug 29, 2023
1 parent 642744b commit fc204e8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 58 deletions.
20 changes: 9 additions & 11 deletions src/UsesFeeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function sitemapUrl(): string
*
* @return array $sitemap
*/
public function sitemapRaw(?string $url = null): array
public function sitemapRaw(string $url = null): array
{
return $this->parseXml($this->fetchAsset($url ?? $this->sitemapUrl()));
}
Expand All @@ -31,11 +31,11 @@ public function sitemapRaw(?string $url = null): array
*
* @return array<FeedEntry> $sitemap
*/
public function sitemap(?string $url = null): array
public function sitemap(string $url = null): array
{
return array_map(
// Create the generic DTO for each
fn ($entry) => FeedEntry::fromArray([
fn ($entry): FeedEntry => FeedEntry::fromArray([
'title' => '',
'description' => '',
'link' => $entry['loc'],
Expand All @@ -59,7 +59,7 @@ public function searchIndexUrl(): string
*
* @return array $searchIndex
*/
public function searchIndexRaw(?string $url = null): array
public function searchIndexRaw(string $url = null): array
{
return $this->parseJson($this->fetchAsset($url ?? $this->searchIndexUrl()));
}
Expand All @@ -69,11 +69,11 @@ public function searchIndexRaw(?string $url = null): array
*
* @return array<FeedEntry> $searchIndex
*/
public function searchIndex(?string $url = null): array
public function searchIndex(string $url = null): array
{
return array_map(
// Create the generic DTO for each
fn ($entry) => FeedEntry::fromArray([
fn ($entry): FeedEntry => FeedEntry::fromArray([
'title' => $entry['title'],
'description' => $entry['snippet'],
'link' => $entry['link'],
Expand All @@ -93,34 +93,32 @@ public function rssUrls(): array
{
$urls = $this->filterExtractAttributes('//link[@type="application/rss+xml"]', ['href']);

return array_map(fn ($url) => (string) $this->makeUrlAbsolute($url), $urls);
return array_map(fn ($url): string => (string) $this->makeUrlAbsolute($url), $urls);
}

/**
* Fetches a given set of RSS feeds and returns one array with raw data.
*
* @param ?string ...$urls
* @return array $rss
*/
public function rssRaw(?string ...$urls): array
{
return array_map(
fn ($url) => $this->parseXml($this->fetchAsset((string) $url)),
empty($urls) ? $this->rssUrls() : $urls
$urls === [] ? $this->rssUrls() : $urls
);
}

/**
* Fetches a given set of RSS feeds and returns one array with raw data.
*
* @param ?string ...$urls
* @return array<FeedEntry> $rss
*/
public function rss(?string ...$urls): array
{
return array_map(
// Create the generic DTO for each
fn ($entry) => FeedEntry::fromArray([
fn ($entry): FeedEntry => FeedEntry::fromArray([
'title' => $entry['title'],
'link' => $entry['link']['@attributes']['href'],
]),
Expand Down
68 changes: 23 additions & 45 deletions src/UsesFileParsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ trait UsesFileParsers
/**
* Base Util to decode a CSV string.
*
* @param ?string $separator
* @param ?string $enclosure
* @param ?string $escape
* @return array $data
*/
public function csvDecodeRaw(
string $csvString,
?string $separator = null,
?string $enclosure = null,
?string $escape = null
string $separator = null,
string $enclosure = null,
string $escape = null
): array {
try {
$csv = array_map(
Expand All @@ -38,23 +35,20 @@ public function csvDecodeRaw(
/**
* Decode CSV and cast types.
*
* @param ?string $separator
* @param ?string $enclosure
* @param ?string $escape
* @return array $data
*/
public function csvDecode(
string $csvString,
?string $separator = null,
?string $enclosure = null,
?string $escape = null
string $separator = null,
string $enclosure = null,
string $escape = null
): array {
try {
$csv = $this->csvDecodeRaw($csvString, $separator, $enclosure, $escape);

// Cast native and custom types
$csv = array_map(
fn ($line) => array_map(
fn ($line): array => array_map(
fn ($cell) => $this->castType($cell),
$line
),
Expand All @@ -70,16 +64,13 @@ public function csvDecode(
/**
* Util to decode a CSV string to asso. array.
*
* @param ?string $separator
* @param ?string $enclosure
* @param ?string $escape
* @return array $data
*/
public function csvDecodeWithHeaderRaw(
string $csvString,
?string $separator = null,
?string $enclosure = null,
?string $escape = null
string $separator = null,
string $enclosure = null,
string $escape = null
): array {
try {
$csv = $this->csvDecodeRaw($csvString, $separator, $enclosure, $escape);
Expand All @@ -104,16 +95,13 @@ function (&$row, $key, $header): void {
/**
* Decode a CSV string to asso. array and cast types.
*
* @param ?string $separator
* @param ?string $enclosure
* @param ?string $escape
* @return array $data
*/
public function csvDecodeWithHeader(
string $csvString,
?string $separator = null,
?string $enclosure = null,
?string $escape = null
string $separator = null,
string $enclosure = null,
string $escape = null
): array {
try {
$csv = $this->csvDecodeWithHeaderRaw($csvString, $separator, $enclosure, $escape);
Expand Down Expand Up @@ -152,17 +140,13 @@ public function castType(string $entry): int|float|string
/**
* Parses a given CSV string or fetches the URL and parses it.
*
* @param ?string $csvStringOrUrl
* @param ?string $separator
* @param ?string $enclosure
* @param ?string $escape
* @return array $data
*/
public function parseCsv(
?string $csvStringOrUrl = null,
?string $separator = null,
?string $enclosure = null,
?string $escape = null
string $csvStringOrUrl = null,
string $separator = null,
string $enclosure = null,
string $escape = null
): array {
// Check if we got either a current page or at least a URL string to process
if ($csvStringOrUrl === null && $this->currentPage === null) {
Expand Down Expand Up @@ -208,17 +192,13 @@ public function parseCsv(
/**
* Parses a given CSV string into an asso. with headers or fetches the URL and parses it.
*
* @param ?string $csvStringOrUrl
* @param ?string $separator
* @param ?string $enclosure
* @param ?string $escape
* @return array $data
*/
public function parseCsvWithHeader(
?string $csvStringOrUrl = null,
?string $separator = null,
?string $enclosure = null,
?string $escape = null
string $csvStringOrUrl = null,
string $separator = null,
string $enclosure = null,
string $escape = null
): array {
// Check if we got either a current page or at least a URL string to process
if ($csvStringOrUrl === null && $this->currentPage === null) {
Expand Down Expand Up @@ -264,10 +244,9 @@ public function parseCsvWithHeader(
/**
* Parses a given JSON string or fetches the URL and parses it.
*
* @param ?string $jsonStringOrUrl
* @return array $data
*/
public function parseJson(?string $jsonStringOrUrl = null): array
public function parseJson(string $jsonStringOrUrl = null): array
{
// Check if we got either a current page or at least a URL string to process
if ($jsonStringOrUrl === null && $this->currentPage === null) {
Expand Down Expand Up @@ -313,10 +292,9 @@ public function parseJson(?string $jsonStringOrUrl = null): array
/**
* Parses a given XML string or fetches the URL and parses it.
*
* @param ?string $xmlStringOrUrl
* @return array $data
*/
public function parseXml(?string $xmlStringOrUrl = null): array
public function parseXml(string $xmlStringOrUrl = null): array
{
// Check if we got either a current page or at least a URL string to process
if ($xmlStringOrUrl === null && $this->currentPage === null) {
Expand Down
4 changes: 2 additions & 2 deletions src/UsesUrls.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function currentUrl(): string
/**
* Returns the current host
*
* @return string $host
* @return string|null $host
*/
public function currentHost(): ?string
{
Expand All @@ -52,7 +52,7 @@ public function currentBaseHost(): string
*
* @return ?string $absoluteUrl
*/
public function makeUrlAbsolute(?string $url = null, string $baseUrl = null): ?string
public function makeUrlAbsolute(string $url = null, string $baseUrl = null): ?string
{
// Allow to pass null through
if ($url === null || $this->currentPage === null) {
Expand Down

0 comments on commit fc204e8

Please sign in to comment.