Skip to content

Commit

Permalink
add type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevie-Ray committed Oct 16, 2022
1 parent 5743fef commit 1a58a53
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,33 @@

namespace StevieRay;

use Algo26\IdnaConvert\Exception\AlreadyPunycodeException;
use Algo26\IdnaConvert\Exception\InvalidCharacterException;
use Algo26\IdnaConvert\ToIdn;
use Algo26\IdnaConvert\EncodingHelper\ToUtf8;
use RuntimeException;

class Generator
{
private $projectUrl = "https://github.com/Stevie-Ray/referrer-spam-blocker";

/** @var string string */
/**
* @var string string
*/
private $outputDir;

/**
* @param string $outputDir
*/
public function __construct($outputDir)
public function __construct(string $outputDir)
{
$this->outputDir = $outputDir;
}

/**
* @throws AlreadyPunycodeException
* @throws InvalidCharacterException
*/
public function generateFiles()
{
$date = date('Y-m-d H:i:s');
Expand All @@ -36,14 +45,16 @@ public function generateFiles()

/**
* @return array
* @throws AlreadyPunycodeException
* @throws InvalidCharacterException
*/
public function domainWorker()
public function domainWorker(): array
{
$domainsFile = __DIR__ . "/domains.txt";

$handle = fopen($domainsFile, "r");
if (!$handle) {
throw new \RuntimeException('Error opening file ' . $domainsFile);
throw new RuntimeException('Error opening file ' . $domainsFile);
}
$lines = array();
$IDN = new ToIdn();
Expand All @@ -67,7 +78,7 @@ public function domainWorker()
$lines[] = $line;
}
fclose($handle);
$uniqueLines = array_unique($lines, SORT_STRING);
$uniqueLines = array_unique($lines);
sort($uniqueLines, SORT_STRING);
if (is_writable($domainsFile)) {
file_put_contents($domainsFile, implode("\n", $uniqueLines));
Expand All @@ -82,7 +93,7 @@ public function domainWorker()
* @param string $filename
* @param string $data
*/
protected function writeToFile($filename, $data)
protected function writeToFile(string $filename, string $data)
{
$file = $this->outputDir . '/' . $filename;
if (is_writable($file)) {
Expand All @@ -91,15 +102,15 @@ protected function writeToFile($filename, $data)
trigger_error("Couldn't not set " . $filename . " permissions to 644");
}
} else {
trigger_error("Permission denied for ${filename}");
trigger_error("Permission denied for $filename");
}
}

/**
* @param string $date
* @param array $lines
*/
public function createApache($date, array $lines)
public function createApache(string $date, array $lines)
{
$data = "# " . $this->projectUrl . "\n# Updated " . $date . "\n\n" .
"<IfModule mod_rewrite.c>\n\nRewriteEngine On\n\n";
Expand Down Expand Up @@ -128,7 +139,7 @@ public function createApache($date, array $lines)
* @param string $date
* @param array $lines
*/
public function createNginx($date, array $lines)
public function createNginx(string $date, array $lines)
{
$data = "# " . $this->projectUrl . "\n# Updated " . $date . "\n#\n# /etc/nginx/referral-spam.conf\n#\n" .
"# With referral-spam.conf in /etc/nginx, include it globally from within /etc/nginx/nginx.conf:\n#\n" .
Expand All @@ -148,7 +159,7 @@ public function createNginx($date, array $lines)
* @param string $date
* @param array $lines
*/
public function createVarnish($date, array $lines)
public function createVarnish(string $date, array $lines)
{
$data = "# " . $this->projectUrl . "\n# Updated " . $date . "\nsub block_referral_spam {\n\tif (\n";
foreach ($lines as $line) {
Expand All @@ -169,7 +180,7 @@ public function createVarnish($date, array $lines)
* @param string $date
* @param array $lines
*/
public function createIIS($date, array $lines)
public function createIIS(string $date, array $lines)
{
$data = "<!-- " . $this->projectUrl . " -->\n<!-- Updated " . $date . " -->\n" .
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
Expand All @@ -192,7 +203,7 @@ public function createIIS($date, array $lines)
* @param string $date
* @param array $lines
*/
public function createuWSGI($date, array $lines)
public function createuWSGI(string $date, array $lines)
{
$data = "# " . $this->projectUrl . "\n# Updated " . $date . "\n#\n" .
"# Put referral-spam.res in /path/to/vassals, then include it from within /path/to/vassals/vassal.ini:\n" .
Expand Down Expand Up @@ -250,7 +261,7 @@ public function createGoogleExclude(array $lines)
* @param string $date
* @param array $lines
*/
public function createCaddyfile($date, array $lines)
public function createCaddyfile(string $date, array $lines)
{
$redir_rules = "";

Expand Down Expand Up @@ -281,7 +292,7 @@ public function createCaddyfile($date, array $lines)
* @param string $date
* @param array $lines
*/
public function createCaddyfileV2($date, array $lines)
public function createCaddyfileV2(string $date, array $lines)
{
$redir_rules = [];

Expand Down

0 comments on commit 1a58a53

Please sign in to comment.