Skip to content

Commit

Permalink
Merge pull request #6 from SandroMiguel/feat/linters-review
Browse files Browse the repository at this point in the history
refactor: refactor assertResourceAvailable method to getNonNullResour…
  • Loading branch information
SandroMiguel authored Mar 14, 2024
2 parents 84a249a + e55581f commit 56ea867
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 36 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@
"outdated:direct": "Check for outdated Composer dependencies that are defined directly in your project.",
"phpcs": "Run PHP CodeSniffer on the source code in src directory.",
"phpcs:file": "Run PHP CodeSniffer on a specific file.",
"phpinsights": "Analyze code quality and architecture with PHP Insights.",
"phpmetrics": "Generate PHP Metrics report and display Node.js version.",
"phpstan": "Run PHPStan analysis with level 7 on the source code in src directory.",
"psalm": "Run Psalm static analysis.",
"phpinsights": "Analyze code quality and architecture with PHP Insights.",
"logs": "Tail the application log located at ./storage/log/app.log.",
"test": "Run PHPUnit tests.",
"test:coverage": "Run PHPUnit tests and generate coverage report in log/report directory."
Expand Down
17 changes: 17 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<psalm
errorLevel="3"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="true"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Base exception class for stream-related exceptions.
*/
class StreamException extends \RuntimeException
abstract class AbstractStreamException extends \RuntimeException
{
/**
* Constructor.
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidStreamException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Exception thrown when an invalid stream resource is provided.
*/
final class InvalidStreamException extends \PhpStreams\Exceptions\StreamException
final class InvalidStreamException extends \PhpStreams\Exceptions\AbstractStreamException
{
/**
* Constructor.
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/ReadException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Exception thrown when an error occurs while reading from a stream.
*/
final class ReadException extends \PhpStreams\Exceptions\StreamException
final class ReadException extends \PhpStreams\Exceptions\AbstractStreamException
{
/**
* Constructor.
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/SeekException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Exception thrown when an error occurs during stream seeking.
*/
final class SeekException extends \PhpStreams\Exceptions\StreamException
final class SeekException extends \PhpStreams\Exceptions\AbstractStreamException
{
/**
* Constructor.
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/WriteException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Exception thrown when an error occurs during stream writing.
*/
final class WriteException extends \PhpStreams\Exceptions\StreamException
final class WriteException extends \PhpStreams\Exceptions\AbstractStreamException
{
/**
* Constructor.
Expand Down
59 changes: 30 additions & 29 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @author Sandro Miguel Marques <[email protected]>
* @link https://github.com/SandroMiguel/php-streams
* @version 1.0.2 (2024-03-13)
* @phpcs:disable SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint
*/

declare(strict_types=1);
Expand All @@ -19,7 +20,7 @@
/**
* Stream class.
*/
class Stream implements StreamInterface
final class Stream implements StreamInterface
{
/** @var resource|null Wrapped resource */
private $resource;
Expand Down Expand Up @@ -87,11 +88,11 @@ public function isWritable(): bool
$meta = \stream_get_meta_data($this->resource);
$mode = $meta['mode'];

return \strstr($mode, 'x')
|| \strstr($mode, 'w')
|| \strstr($mode, 'c')
|| \strstr($mode, 'a')
|| \strstr($mode, '+');
return \strstr($mode, 'x') !== false
|| \strstr($mode, 'w') !== false
|| \strstr($mode, 'c') !== false
|| \strstr($mode, 'a') !== false
|| \strstr($mode, '+') !== false;
}

/**
Expand All @@ -108,7 +109,7 @@ public function isReadable(): bool
$meta = \stream_get_meta_data($this->resource);
$mode = $meta['mode'];

return \strstr($mode, 'r') || \strstr($mode, '+');
return \strstr($mode, 'r') !== false || \strstr($mode, '+') !== false;
}

/**
Expand All @@ -125,7 +126,7 @@ public function isReadable(): bool
*/
public function read(int $length): string
{
$this->assertResourceAvailable();
$resource = $this->getNonNullResourceOrFail();

if (!$this->isReadable()) {
throw new \PhpStreams\Exceptions\ReadException(
Expand All @@ -135,7 +136,7 @@ public function read(int $length): string

$length = \max(0, $length);

$result = \fread($this->resource, $length);
$result = \fread($resource, $length);

if ($result === false) {
throw new \PhpStreams\Exceptions\ReadException(
Expand All @@ -157,15 +158,15 @@ public function read(int $length): string
*/
public function write(string $string): int
{
$this->assertResourceAvailable();
$resource = $this->getNonNullResourceOrFail();

if (!$this->isWritable()) {
throw new \PhpStreams\Exceptions\WriteException(
'Stream is not writable'
);
}

$result = \fwrite($this->resource, $string);
$result = \fwrite($resource, $string);

if ($result === false) {
throw new \PhpStreams\Exceptions\WriteException(
Expand Down Expand Up @@ -212,9 +213,9 @@ public function eof(): bool
*/
public function tell(): int
{
$this->assertResourceAvailable();
$resource = $this->getNonNullResourceOrFail();

$result = \ftell($this->resource);
$result = \ftell($resource);

if ($result === false) {
throw new \RuntimeException('Unable to determine stream position');
Expand All @@ -229,21 +230,19 @@ public function tell(): int
* If the stream is not seekable, this method will raise an exception;
* otherwise, it will perform a seek(0).
*
* @throws \PhpStreams\Exceptions\ReadException If resource is not
* available.
* @throws \PhpStreams\Exceptions\SeekException If stream is not seekable.
*/
public function rewind(): void
{
$this->assertResourceAvailable();
$resource = $this->getNonNullResourceOrFail();

if (!$this->isSeekable()) {
throw new \PhpStreams\Exceptions\SeekException(
'Stream is not seekable.'
);
}

$result = \fseek($this->resource, 0, \SEEK_SET);
$result = \fseek($resource, 0, \SEEK_SET);

if ($result === -1) {
throw new \PhpStreams\Exceptions\SeekException(
Expand Down Expand Up @@ -277,15 +276,15 @@ public function getSize(): ?int
*/
public function getContents(): string
{
$this->assertResourceAvailable();
$resource = $this->getNonNullResourceOrFail();

if (\get_resource_type($this->resource) !== 'stream') {
if (\get_resource_type($resource) !== 'stream') {
throw new \PhpStreams\Exceptions\ReadException(
'Unable to read from stream: supplied resource is not a valid stream resource.'
);
}

$result = \stream_get_contents($this->resource);
$result = \stream_get_contents($resource);

if ($result === false) {
throw new \PhpStreams\Exceptions\ReadException();
Expand Down Expand Up @@ -313,19 +312,17 @@ public function detach()
* @param int $offset Stream offset
* @param int $whence Specifies how the cursor position will be calculated
* based on the seek offset. Valid values are identical to the built-in
* PHP $whence values for `fseek()`. SEEK_SET: Set position equal to
* PHP $whence values for `fseek()`. SEEK_SET: Set position equal to
* offset bytes SEEK_CUR: Set position to current location plus offset
* SEEK_END: Set position to end-of-stream plus offset.
*
* @throws \PhpStreams\Exceptions\ReadException If resource is not
* available.
* @throws \PhpStreams\Exceptions\SeekException If stream is not seekable.
* @throws \PhpStreams\Exceptions\InvalidStreamException If invalid seek
* offset is specified.
*/
public function seek(int $offset, int $whence = \SEEK_SET): void
{
$this->assertResourceAvailable();
$resource = $this->getNonNullResourceOrFail();

if (!$this->isSeekable()) {
throw new \PhpStreams\Exceptions\SeekException(
Expand All @@ -339,7 +336,7 @@ public function seek(int $offset, int $whence = \SEEK_SET): void
);
}

$result = \fseek($this->resource, $offset, $whence);
$result = \fseek($resource, $offset, $whence);

if ($result === -1) {
throw new \PhpStreams\Exceptions\SeekException();
Expand All @@ -354,9 +351,9 @@ public function seek(int $offset, int $whence = \SEEK_SET): void
*
* @param string $key Specific metadata to retrieve.
*
* @return array|mixed|null Returns an associative array if no key is
* provided. Returns a specific key value if a key is provided and the
* value is found, or null if the key is not found.
* @return array<string,mixed>|mixed|null Returns an associative array if
* no key is provided. Returns a specific key value if a key is provided
* and the value is found, or null if the key is not found.
*/
public function getMetadata(?string $key = null): mixed
{
Expand All @@ -378,16 +375,20 @@ public function getMetadata(?string $key = null): mixed
/**
* Asserts that a valid resource is available for stream operations.
*
* @return resource Returns a non-null resource.
*
* @throws \PhpStreams\Exceptions\ReadException If the resource is not
* available.
*/
private function assertResourceAvailable(): void
private function getNonNullResourceOrFail()
{
if ($this->resource === null) {
throw new \PhpStreams\Exceptions\ReadException(
'Unable to read from stream: resource is not available.'
);
}

return $this->resource;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/tests/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testConstructorWithInvalidResource(): void
'Invalid or non-stream resource provided. Provided resource type: non-resource'
);

new Stream('invalid_resource');
new Stream(false);
}

/**
Expand Down

0 comments on commit 56ea867

Please sign in to comment.