Skip to content

Commit

Permalink
Improve internal codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Sep 25, 2024
1 parent f14b525 commit 8b8ea7f
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 87 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@
"ext-dom": "*",
"ext-xdebug": "*",
"friendsofphp/php-cs-fixer": "^3.64.0",
"illuminate/collections": "^11.24.1",
"phpbench/phpbench": "^1.3.1",
"phpstan/phpstan": "^1.12.4",
"phpstan/phpstan-deprecation-rules": "^1.2.1",
"phpstan/phpstan-phpunit": "^1.4.0",
"phpstan/phpstan-strict-rules": "^1.6.0",
"phpstan/phpstan-strict-rules": "^1.6.1",
"phpunit/phpunit": "^10.5.16 || ^11.3.6",
"symfony/var-dumper": "^6.4.6 || ^7.1.4"
"symfony/var-dumper": "^6.4.6 || ^7.1.5"
},
"autoload": {
"psr-4": {
Expand Down
12 changes: 3 additions & 9 deletions src/AbstractCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,7 @@ public function setDelimiter(string $delimiter): static
return $this;
}

if (1 !== strlen($delimiter)) {
throw InvalidArgument::dueToInvalidDelimiterCharacter($delimiter, __METHOD__);
}
1 === strlen($delimiter) || throw InvalidArgument::dueToInvalidDelimiterCharacter($delimiter, __METHOD__);

$this->delimiter = $delimiter;
$this->resetProperties();
Expand All @@ -338,9 +336,7 @@ public function setEnclosure(string $enclosure): static
return $this;
}

if (1 !== strlen($enclosure)) {
throw InvalidArgument::dueToInvalidEnclosureCharacter($enclosure, __METHOD__);
}
1 === strlen($enclosure) || throw InvalidArgument::dueToInvalidEnclosureCharacter($enclosure, __METHOD__);

$this->enclosure = $enclosure;
$this->resetProperties();
Expand Down Expand Up @@ -418,9 +414,7 @@ public function setOutputBOM(Bom|string|null $str): static
*/
public function addStreamFilter(string $filtername, null|array $params = null): static
{
if (!$this->document instanceof Stream) {
throw UnavailableFeature::dueToUnsupportedStreamFilterApi(get_class($this->document));
}
$this->document instanceof Stream || throw UnavailableFeature::dueToUnsupportedStreamFilterApi(get_class($this->document));

$this->document->appendFilter($filtername, static::STREAM_FILTER_MODE, $params);
$this->stream_filters[$filtername] = true;
Expand Down
4 changes: 1 addition & 3 deletions src/ColumnConsistency.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ class ColumnConsistency
public function __construct(
protected int $columns_count = -1
) {
if ($this->columns_count < -1) {
throw InvalidArgument::dueToInvalidColumnCount($this->columns_count, __METHOD__);
}
$this->columns_count >= -1 || throw InvalidArgument::dueToInvalidColumnCount($this->columns_count, __METHOD__);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/EscapeFormula.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ public function __construct(
protected function filterSpecialCharacters(string ...$characters): array
{
foreach ($characters as $str) {
if (1 !== strlen($str)) {
throw new InvalidArgumentException('The submitted string '.$str.' must be a single character');
}
1 === strlen($str) || throw new InvalidArgumentException('The submitted string '.$str.' must be a single character');
}

return $characters;
Expand Down
5 changes: 2 additions & 3 deletions src/HTMLConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ protected function addHTMLAttributes(DOMElement $node): void
*/
public function table(string $class_name, string $id_value = ''): self
{
if (1 === preg_match(",\s,", $id_value)) {
throw new DOMException("The id attribute's value must not contain whitespace (spaces, tabs etc.)");
}
1 !== preg_match(",\s,", $id_value) || throw new DOMException("The id attribute's value must not contain whitespace (spaces, tabs etc.)");

$clone = clone $this;
$clone->class_name = $class_name;
$clone->id_value = $id_value;
Expand Down
8 changes: 2 additions & 6 deletions src/JsonConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,9 @@ private function __construct(
?Closure $formatter
) {
json_encode([], $flags & ~JSON_THROW_ON_ERROR, $depth);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidArgumentException('The flags or the depth given are not valid JSON encoding parameters in PHP; '.json_last_error_msg());
}

if (1 > $indentSize) {
throw new InvalidArgumentException('The indentation space must be greater or equal to 1.');
}
JSON_ERROR_NONE === json_last_error() || throw new InvalidArgumentException('The flags or the depth given are not valid JSON encoding parameters in PHP; '.json_last_error_msg());
1 <= $indentSize || throw new InvalidArgumentException('The indentation space must be greater or equal to 1.');

$this->flags = $flags;
$this->depth = $depth;
Expand Down
4 changes: 1 addition & 3 deletions src/Query/Constraint/TwoColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ private function __construct(
public readonly Comparison|Closure $operator,
public readonly array|string|int $second,
) {
if ($this->operator instanceof Closure && is_array($this->second)) {
throw new QueryException('The second column must be a string if the operator is a callback.');
}
!$this->operator instanceof Closure || !is_array($this->second) || throw new QueryException('The second column must be a string if the operator is a callback.');

if (is_array($this->second)) {
$res = array_filter($this->second, fn (mixed $value): bool => !is_string($value) && !is_int($value));
Expand Down
9 changes: 2 additions & 7 deletions src/Query/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@ private function __construct(
public readonly int $offset,
public readonly int $length,
) {
if (0 > $this->offset) {
throw new QueryException(self::class.' expects the offset to be greater or equal to 0, '.$this->offset.' given.');
}

if (-1 > $this->length) {
throw new QueryException(self::class.' expects the length to be greater or equal to -1, '.$this->length.' given.');
}
0 <= $this->offset || throw new QueryException(self::class.' expects the offset to be greater or equal to 0, '.$this->offset.' given.');
-2 < $this->length || throw new QueryException(self::class.' expects the length to be greater or equal to -1, '.$this->length.' given.');
}

public static function new(int $offset, int $length): self
Expand Down
4 changes: 1 addition & 3 deletions src/Query/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ private static function getObjectPropertyValue(object $row, string|int ...$keys)
continue;
}

if (is_int($key)) {
throw QueryException::dueToUnknownColumn($key, $row);
}
!is_int($key) || throw QueryException::dueToUnknownColumn($key, $row);

if ($object->hasProperty($key) && $object->getProperty($key)->isPublic()) {
$res[$key] = $object->getProperty($key)->getValue($row);
Expand Down
8 changes: 2 additions & 6 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ public function setHeaderOffset(?int $offset): static
return $this;
}

if (null !== $offset && 0 > $offset) {
throw InvalidArgument::dueToInvalidHeaderOffset($offset, __METHOD__);
}
null === $offset || -1 < $offset || throw InvalidArgument::dueToInvalidHeaderOffset($offset, __METHOD__);

$this->header_offset = $offset;
$this->resetProperties();
Expand Down Expand Up @@ -536,9 +534,7 @@ protected function stripBOM(Iterator $iterator, ?Bom $bom): Iterator
*/
protected function prepareHeader($header = []): array
{
if ($header !== (array_filter($header, is_string(...)))) {
throw SyntaxError::dueToInvalidHeaderColumnNames();
}
$header == array_filter($header, is_string(...)) || throw SyntaxError::dueToInvalidHeaderColumnNames();

return $this->computeHeader($header);
}
Expand Down
17 changes: 4 additions & 13 deletions src/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ class ResultSet implements TabularDataReader, JsonSerializable
*/
public function __construct(Iterator|array $records, array $header = [])
{
if ($header !== array_filter($header, is_string(...))) {
throw SyntaxError::dueToInvalidHeaderColumnNames();
}
$header === array_filter($header, is_string(...)) || throw SyntaxError::dueToInvalidHeaderColumnNames();

$this->header = array_values($this->validateHeader($header));
$this->records = match (true) {
Expand Down Expand Up @@ -174,9 +172,7 @@ public function reduce(Closure $callback, mixed $initial = null): mixed
*/
public function chunkBy(int $recordsCount): iterable
{
if ($recordsCount < 1) {
throw InvalidArgument::dueToInvalidChunkSize($recordsCount, __METHOD__);
}
$recordsCount > 0 || throw InvalidArgument::dueToInvalidChunkSize($recordsCount, __METHOD__);

$header = $this->getHeader();
$records = [];
Expand Down Expand Up @@ -323,10 +319,7 @@ public function getRecordsAsObject(string $className, array $header = []): Itera
*/
protected function prepareHeader(array $header): array
{
if ($header !== array_filter($header, is_string(...))) {
throw SyntaxError::dueToInvalidHeaderColumnNames();
}

$header === array_filter($header, is_string(...)) || throw SyntaxError::dueToInvalidHeaderColumnNames();
$header = $this->validateHeader($header);
if ([] === $header) {
$header = $this->header;
Expand Down Expand Up @@ -382,9 +375,7 @@ public function value(int|string $column = 0): mixed

public function nth(int $nth_record): array
{
if ($nth_record < 0) {
throw InvalidArgument::dueToInvalidRecordOffset($nth_record, __METHOD__);
}
0 <= $nth_record || throw InvalidArgument::dueToInvalidRecordOffset($nth_record, __METHOD__);

$iterator = new LimitIterator($this->getIterator(), $nth_record, 1);
$iterator->rewind();
Expand Down
5 changes: 1 addition & 4 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,7 @@ public function offset(int $offset): self
*/
public function limit(int $limit): self
{
if (-1 > $limit) {
throw InvalidArgument::dueToInvalidLimit($limit, __METHOD__);
}

$limit >= -1 || throw InvalidArgument::dueToInvalidLimit($limit, __METHOD__);
if ($limit === $this->limit) {
return $this;
}
Expand Down
25 changes: 6 additions & 19 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ public static function createFromPath(string $path, string $open_mode = 'r', $co
$resource = fopen(...$args);
restore_error_handler();

if (!is_resource($resource)) {
throw UnavailableStream::dueToPathNotFound($path);
}
is_resource($resource) || throw UnavailableStream::dueToPathNotFound($path);

$instance = new self($resource);
$instance->should_close_stream = true;
Expand Down Expand Up @@ -187,9 +185,7 @@ public function appendFilter(string $filtername, int $read_write, ?array $params
set_error_handler(fn (int $errno, string $errstr, string $errfile, int $errline) => true);
$res = stream_filter_append($this->stream, $filtername, $read_write, $params ?? []);
restore_error_handler();
if (!is_resource($res)) {
throw InvalidArgument::dueToStreamFilterNotFound($filtername);
}
is_resource($res) || throw InvalidArgument::dueToStreamFilterNotFound($filtername);

$this->filters[$filtername][] = $res;
}
Expand Down Expand Up @@ -292,13 +288,8 @@ public function next(): void
*/
public function rewind(): void
{
if (!$this->is_seekable) {
throw UnavailableFeature::dueToMissingStreamSeekability();
}

if (false === rewind($this->stream)) {
throw new RuntimeException('Unable to rewind the document.');
}
$this->is_seekable || throw UnavailableFeature::dueToMissingStreamSeekability();
false !== rewind($this->stream) || throw new RuntimeException('Unable to rewind the document.');

$this->offset = 0;
$this->value = false;
Expand Down Expand Up @@ -355,9 +346,7 @@ public function fgets(): string|false
*/
public function setMaxLineLen(int $maxLength): void
{
if (0 > $maxLength) {
throw new ValueError(' Argument #1 ($maxLength) must be greater than or equal to 0');
}
0 <= $maxLength || throw new ValueError(' Argument #1 ($maxLength) must be greater than or equal to 0');

$this->maxLength = $maxLength;
}
Expand Down Expand Up @@ -429,9 +418,7 @@ private function getCurrentLine(): string|false
*/
public function seek(int $offset): void
{
if ($offset < 0) {
throw InvalidArgument::dueToInvalidSeekingPosition($offset, __METHOD__);
}
$offset >= 0 || throw InvalidArgument::dueToInvalidSeekingPosition($offset, __METHOD__);

$this->rewind();
while ($this->key() !== $offset && $this->valid()) {
Expand Down
8 changes: 2 additions & 6 deletions src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ public function insertOne(array $record): int
protected function validateRecord(array $record): void
{
foreach ($this->validators as $name => $validator) {
if (true !== $validator($record)) {
throw CannotInsertRecord::triggerOnValidation($name, $record);
}
true === $validator($record) || throw CannotInsertRecord::triggerOnValidation($name, $record);
}
}

Expand Down Expand Up @@ -192,9 +190,7 @@ public function setFlushThreshold(?int $threshold): self
return $this;
}

if (null !== $threshold && 1 > $threshold) {
throw InvalidArgument::dueToInvalidThreshold($threshold, __METHOD__);
}
null === $threshold || 1 <= $threshold || throw InvalidArgument::dueToInvalidThreshold($threshold, __METHOD__);

$this->flush_threshold = $threshold;
$this->flush_counter = 0;
Expand Down

0 comments on commit 8b8ea7f

Please sign in to comment.