Skip to content

phpstan support #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
level: 7
checkGenericClassInNonGenericObjectType: false
paths:
- src
- tests
62 changes: 54 additions & 8 deletions src/CsvFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ public function __construct(
) {
}

/**
* @param string $filename
* @param string $keyword
* @param string $column
* @param string|null $format
* @return bool|array<string,string>
* @throws FileHandlerException
*/
public function searchInCsvFile(
string $filename,
string $keyword,
Expand All @@ -32,13 +40,18 @@ public function searchInCsvFile(
}


public function toJson(string $filename): string
public function toJson(string $filename): string|false
{
$data = $this->toArray($filename);

return json_encode($data);
}

/**
* @param string $filename
* @return array<int,array<string,string>>
* @throws FileHandlerException
*/
public function toArray(string $filename): array
{
if (!file_exists($filename)) {
Expand All @@ -61,6 +74,9 @@ public function findAndReplaceInCsv(
}

$tempFilePath = $this->tempFileHandler->createTempFileWithHeaders($headers);
if (!$tempFilePath) {
return false;
}


try {
Expand All @@ -86,22 +102,24 @@ public function findAndReplaceInCsv(
return true;
}

/**
* @param mixed $file
* @return array<string>|false
*/

private function extractHeader(mixed $file): array|false
{
$headers = [];
if (is_resource($file)) {
$headers = fgetcsv($file);
}
if (is_string($file)) {
if (!file_exists($file)) {
$file = fopen($file, 'r');
if (!$file) {
return false;
}
try {
$file = fopen($file, 'r');
$headers = fgetcsv($file);
} finally {
fclose($file);
}
$headers = fgetcsv($file);
fclose($file);
}

if (!$headers) {
Expand All @@ -116,6 +134,10 @@ private function extractHeader(mixed $file): array|false
return $headers;
}

/**
* @param array<string> $row
* @return bool
*/
private function isValidCsvFileFormat(array $row): bool
{
if (count($row) <= 1) {
Expand All @@ -124,10 +146,21 @@ private function isValidCsvFileFormat(array $row): bool
return true;
}

/**
* @param string $filename
* @return Generator
* @throws FileHandlerException
*/
private function getRows(string $filename): Generator
{
$csvFile = fopen($filename, 'r');
if (!$csvFile) {
throw new FileHandlerException('file not found');
}
$headers = $this->extractHeader($csvFile);
if (!is_array($headers)) {
throw new FileHandlerException('could not extract header');
}


$isEmptyFile = true;
Expand All @@ -151,6 +184,12 @@ private function getRows(string $filename): Generator
}
}

/**
* @param array<string> $row
* @param string $keyword
* @param string $replace
* @return int
*/
private function replaceKeywordInRow(array &$row, string $keyword, string $replace): int
{
$count = 0;
Expand All @@ -164,6 +203,13 @@ private function replaceKeywordInRow(array &$row, string $keyword, string $repla
return $count;
}

/**
* @param array<string> $row
* @param string $column
* @param string $keyword
* @param string $replace
* @return int
*/
private function replaceKeywordInColumn(array &$row, string $column, string $keyword, string $replace): int
{
$count = 0;
Expand Down
9 changes: 9 additions & 0 deletions src/FileEncryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public function encryptFile(): bool
$output = bin2hex($nonce . $ciphertext);

$file = fopen($this->filename, 'w');
if (!$file) {
return false;
}

try {
fwrite($file, $output);
Expand Down Expand Up @@ -76,6 +79,9 @@ public function decryptFile(): bool
}

$bytes = hex2bin($encryptedData);
if (!$bytes) {
return false;
}
$nonce = substr($bytes, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$ciphertext = substr($bytes, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);

Expand All @@ -89,6 +95,9 @@ public function decryptFile(): bool


$file = fopen($this->filename, 'w');
if (!$file) {
return false;
}

try {
fwrite($file, $plaintext);
Expand Down
29 changes: 7 additions & 22 deletions src/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ class FileHandler
{
public const ARRAY_FORMAT = 'array';

/**
* @var array<resource>|null
*/
private null|array $files = [];


/**
* @throws FileHandlerException
*/
public function open(
string $filename,
string $mode = "w",
bool $include_path = false,
$context = null
mixed $context = null
): self {
$file = fopen($filename, $mode, $include_path, $context);

Expand All @@ -35,6 +35,9 @@ public function open(


/**
* @param string $data
* @param int<0, max>|null $length
* @return void
* @throws FileHandlerException
*/
public function write(string $data, ?int $length = null): void
Expand Down Expand Up @@ -144,22 +147,4 @@ public function delete(string $filename): void
}
unlink($filename);
}


public function getSingleFileProcessing(string|null $filename): mixed
{
if (empty($this->files)) {
if ($filename && file_exists($filename)) {
$this->open($filename);
return $this->files[0];
}
throw new FileHandlerException("No files to process or file not found: $filename");
}
if (count($this->files) > 1) {
throw new FileHandlerException("Multiple files not allowed");
}


return $this->files[0];
}
}
7 changes: 3 additions & 4 deletions src/FileHashChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

class FileHashChecker
{
const ALGO_256 = 'sha3-256';
const ALGO_512 = 'sha3-512';
public const ALGO_256 = 'sha3-256';
public const ALGO_512 = 'sha3-512';

/**
* @param string $filename
Expand All @@ -21,7 +21,6 @@ public function __construct(private readonly string $filename, private readonly
}

/**
* @param object $fileHandler
* @param string $storedHashesFile
* @param string $algo
* @return bool
Expand All @@ -42,7 +41,7 @@ public function verifyHash(string $storedHashesFile, string $algo = self::ALGO_2
format: FileHandler::ARRAY_FORMAT
);

if (!$file) {
if (!$file || !is_array($file)) {
throw new HashException('this file is not hashed');
}

Expand Down
21 changes: 15 additions & 6 deletions src/StreamHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@

class StreamHandler
{
/**
* @var array<Fiber> $fibers
*/
private array $fibers = [];


/**
* @param array<string,string> $streamUrls
* @param int<0,100> $chunk
* @throws StreamException
*/
public function __construct(public readonly array $streamUrls, public readonly int $chunk = 100)
Expand All @@ -21,23 +26,27 @@ public function __construct(public readonly array $streamUrls, public readonly i
}
}

/**
*/

private function stream(string $streamUrl, string $outputFilename): Fiber
{
return new Fiber(function () use ($streamUrl, $outputFilename) {
$stream = fopen($streamUrl, 'r');
if (!$stream) {
$outputFile = fopen($outputFilename, 'w');
if (!$stream || !$outputFile) {
throw new StreamException("Failed to open stream: $streamUrl");
}

stream_set_blocking($stream, false);

$outputFile = fopen($outputFilename, 'w');

try {
while (!feof($stream)) {
$contents = fread($stream, $this->chunk);
fwrite($outputFile, $contents);
$length = $this->chunk;
$contents = fread($stream, $length);
if ($contents) {
fwrite($outputFile, $contents);
}

Fiber::suspend();
}
} catch (Throwable $e) {
Expand Down
27 changes: 22 additions & 5 deletions src/TempFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,36 @@ public function cleanupTempFile(string $tempFilePath): void
}
}

/**
* @param string $tempFilePath
* @param array<string> $row
* @return void
*/
public function writeRowToTempFile(string $tempFilePath, array $row): void
{
$tempFileHandle = fopen($tempFilePath, 'a');
fputs($tempFileHandle, implode(',', $row) . PHP_EOL);
fclose($tempFileHandle);
if ($tempFileHandle) {
fputs($tempFileHandle, implode(',', $row) . PHP_EOL);
fclose($tempFileHandle);
}
}

public function createTempFileWithHeaders(array $headers): string
/**
* @param array<string> $headers
* @return string
*/
public function createTempFileWithHeaders(array $headers): string|false
{
$tempFilePath = tempnam(sys_get_temp_dir(), 'tempfile_');
if (!$tempFilePath) {
return false;
}
$tempFileHandle = fopen($tempFilePath, 'w');
fputs($tempFileHandle, implode(',', $headers) . PHP_EOL);
fclose($tempFileHandle);
if ($tempFileHandle) {
fputs($tempFileHandle, implode(',', $headers) . PHP_EOL);
fclose($tempFileHandle);
}


return $tempFilePath;
}
Expand Down
23 changes: 22 additions & 1 deletion tests/Base/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@

class BaseTest extends TestCase
{
/**
* @var array<string>|null
*/
protected static array|null $files = [];

protected static ContainerBuilder|null $containerBuilder;
private static ContainerBuilder|null $containerBuilder;

public static function setUpBeforeClass(): void
{
Expand All @@ -38,4 +41,22 @@ public static function tearDownAfterClass(): void
}
static::$files = null;
}

protected function isFileValid(string $filename): mixed
{
if (!file_exists($filename) || !$data = file_get_contents($filename)) {
$this->fail('file does not exists or has no content');
}
return $data;
}

protected function setObjectHandler(string $classname, string $serviceId): mixed
{
$objectHandler = self::$containerBuilder->get($serviceId);

if (!is_a($objectHandler, $classname)) {
$this->fail("provided service is not an instance of " . $classname);
}
return $objectHandler;
}
}
Loading