diff --git a/composer.lock b/composer.lock index 7ca34b0b..6e2ae33d 100644 --- a/composer.lock +++ b/composer.lock @@ -1464,11 +1464,11 @@ }, { "name": "phpstan/phpstan", - "version": "2.1.39", + "version": "2.1.40", "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/c6f73a2af4cbcd99c931d0fb8f08548cc0fa8224", - "reference": "c6f73a2af4cbcd99c931d0fb8f08548cc0fa8224", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/9b2c7aeb83a75d8680ea5e7c9b7fca88052b766b", + "reference": "9b2c7aeb83a75d8680ea5e7c9b7fca88052b766b", "shasum": "" }, "require": { @@ -1513,7 +1513,7 @@ "type": "github" } ], - "time": "2026-02-11T14:48:56+00:00" + "time": "2026-02-23T15:04:35+00:00" }, { "name": "phpstan/phpstan-strict-rules", @@ -3201,32 +3201,32 @@ }, { "name": "slevomat/coding-standard", - "version": "8.27.1", + "version": "8.28.0", "source": { "type": "git", "url": "https://github.com/slevomat/coding-standard.git", - "reference": "29bdaee8b65e7ed2b8e702b01852edba8bae1769" + "reference": "0cd4b30cc1037eca54091c188d260d570e61770c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/slevomat/coding-standard/zipball/29bdaee8b65e7ed2b8e702b01852edba8bae1769", - "reference": "29bdaee8b65e7ed2b8e702b01852edba8bae1769", + "url": "https://api.github.com/repos/slevomat/coding-standard/zipball/0cd4b30cc1037eca54091c188d260d570e61770c", + "reference": "0cd4b30cc1037eca54091c188d260d570e61770c", "shasum": "" }, "require": { "dealerdirect/phpcodesniffer-composer-installer": "^0.7 || ^1.2.0", "php": "^7.4 || ^8.0", - "phpstan/phpdoc-parser": "^2.3.1", + "phpstan/phpdoc-parser": "^2.3.2", "squizlabs/php_codesniffer": "^4.0.1" }, "require-dev": { - "phing/phing": "3.0.1|3.1.1", + "phing/phing": "3.0.1|3.1.2", "php-parallel-lint/php-parallel-lint": "1.4.0", - "phpstan/phpstan": "2.1.37", - "phpstan/phpstan-deprecation-rules": "2.0.3", - "phpstan/phpstan-phpunit": "2.0.12", - "phpstan/phpstan-strict-rules": "2.0.7", - "phpunit/phpunit": "9.6.31|10.5.60|11.4.4|11.5.49|12.5.7" + "phpstan/phpstan": "2.1.40", + "phpstan/phpstan-deprecation-rules": "2.0.4", + "phpstan/phpstan-phpunit": "2.0.16", + "phpstan/phpstan-strict-rules": "2.0.10", + "phpunit/phpunit": "9.6.34|10.5.63|11.4.4|11.5.50|12.5.14" }, "type": "phpcodesniffer-standard", "extra": { @@ -3250,7 +3250,7 @@ ], "support": { "issues": "https://github.com/slevomat/coding-standard/issues", - "source": "https://github.com/slevomat/coding-standard/tree/8.27.1" + "source": "https://github.com/slevomat/coding-standard/tree/8.28.0" }, "funding": [ { @@ -3262,7 +3262,7 @@ "type": "tidelift" } ], - "time": "2026-01-25T15:57:07+00:00" + "time": "2026-02-23T21:35:24+00:00" }, { "name": "squizlabs/php_codesniffer", diff --git a/src/Shared/Environment/EnvironmentReader.php b/src/Shared/Environment/EnvironmentReader.php index b9039a46..fdf9fd1e 100644 --- a/src/Shared/Environment/EnvironmentReader.php +++ b/src/Shared/Environment/EnvironmentReader.php @@ -10,8 +10,8 @@ { public function getProjectRoot(): string { - $projectRoot = $this->getEnvironment(EnvironmentEnum::PROJECT_ROOT) - ?: $this->getEnvironment(EnvironmentEnum::PROJECT_ROOT_ALIAS); + $projectRoot = $this->getEnvironment(@EnvironmentEnum::PROJECT_ROOT) + ?: $this->getEnvironment(@EnvironmentEnum::PROJECT_ROOT_ALIAS); if ($projectRoot === '') { return $this->getcwd() ?: ''; @@ -22,9 +22,24 @@ public function getProjectRoot(): string return rtrim($projectRoot, '\/'); } - public function getMaxFileSizeMegabytes(): string + public function getMaxFileSizeMegabytes(): int { - return $this->getEnvironment(EnvironmentEnum::MAX_FILE_SIZE_MB); + $environment = EnvironmentEnum::MAX_FILE_SIZE_MB; + $maxFileSize = (int)$this->getEnvironment($environment); + + if ($maxFileSize === 0) { + $maxFileSize = (int)$environment->getDefault(); + } + + /** @var int $maxFileSize */ + $maxFileSize = min($maxFileSize, 1024); + + return $maxFileSize; + } + + public function getMaxFileSizeBytes(): int + { + return $this->getMaxFileSizeMegabytes() * 1_000_000; } private function getEnvironment(EnvironmentEnum $environment): string diff --git a/src/Shared/Environment/EnvironmentReaderInterface.php b/src/Shared/Environment/EnvironmentReaderInterface.php index aac44eb2..264179e5 100644 --- a/src/Shared/Environment/EnvironmentReaderInterface.php +++ b/src/Shared/Environment/EnvironmentReaderInterface.php @@ -8,5 +8,7 @@ interface EnvironmentReaderInterface { public function getProjectRoot(): string; - public function getMaxFileSizeMegabytes(): string; + public function getMaxFileSizeMegabytes(): int; + + public function getMaxFileSizeBytes(): int; } diff --git a/src/Shared/Validator/FileSizeValidator.php b/src/Shared/Validator/FileSizeValidator.php index 9fe2e232..154eb886 100644 --- a/src/Shared/Validator/FileSizeValidator.php +++ b/src/Shared/Validator/FileSizeValidator.php @@ -16,8 +16,6 @@ File "%s" ("%d" bytes) exceeds the maximum allowed size of "%d" bytes. Please split the file into smaller parts. TEMPLATE; - private const int MAX_FILE_SIZE_BYTE_MULTIPLIER = 1_000_000; - public function __construct(private EnvironmentReaderInterface $environmentReader) { } @@ -49,7 +47,7 @@ public function validate(string $path): ?ValidatorMessageTransfer private function getMaxFileSizeBytes(): int { - return (int)$this->environmentReader->getMaxFileSizeMegabytes() * self::MAX_FILE_SIZE_BYTE_MULTIPLIER; + return $this->environmentReader->getMaxFileSizeBytes(); } protected function filesize(string $path): int|false diff --git a/tests/unit/Shared/Environment/EnvironmentReaderTest.php b/tests/unit/Shared/Environment/EnvironmentReaderTest.php index e036b09c..469cc3d5 100644 --- a/tests/unit/Shared/Environment/EnvironmentReaderTest.php +++ b/tests/unit/Shared/Environment/EnvironmentReaderTest.php @@ -119,4 +119,26 @@ public function testProjectRootVariableIsNotSetAndWorkingDirectoryIsFailed(): vo // Assert $this->assertSame($expected, $actual); } + + #[TestDox('Environment variable max file size megabytes "$maxSizeMegabytes" is set with "$expected"')] + #[TestWith([false, 10])] + #[TestWith(['', 10])] + #[TestWith(['0', 10])] + #[TestWith(['test', 10])] + #[TestWith(['20', 20])] + #[TestWith(['200000', 1024])] + public function testGetMaxFileSizeMegabytes(string|bool $maxSizeMegabytes, int $expected): void + { + // Expect + $this->readerMock->expects($this->once()) + ->method('getenv') + ->willReturn($maxSizeMegabytes) + ->seal(); + + // Act + $actual = $this->readerMock->getMaxFileSizeMegabytes(); + + // Assert + $this->assertSame($expected, $actual); + } } diff --git a/tests/unit/Shared/Validator/FileSizeValidatorTest.php b/tests/unit/Shared/Validator/FileSizeValidatorTest.php index 01a8f114..2056ab33 100644 --- a/tests/unit/Shared/Validator/FileSizeValidatorTest.php +++ b/tests/unit/Shared/Validator/FileSizeValidatorTest.php @@ -12,7 +12,7 @@ final class FileSizeValidatorTest extends TestCase { - private const string MAX_FILE_SIZE_MEGABYTES = '1'; + private const int MAX_FILE_SIZE_BYTES = 10; private FileSizeValidator&MockObject $validatorMock; @@ -20,8 +20,8 @@ protected function setUp(): void { $environmentReaderStub = $this->createStub(EnvironmentReaderInterface::class); $environmentReaderStub - ->method('getMaxFileSizeMegabytes') - ->willReturn(self::MAX_FILE_SIZE_MEGABYTES) + ->method('getMaxFileSizeBytes') + ->willReturn(self::MAX_FILE_SIZE_BYTES) ->seal(); $this->validatorMock = $this->getMockBuilder(FileSizeValidator::class) @@ -56,7 +56,7 @@ public function testMaxFileSizeExceeded(): void { // Arrange $path = '/some-path.txt'; - $fileSize = (int)self::MAX_FILE_SIZE_MEGABYTES * 1_000_000 + 10; + $fileSize = self::MAX_FILE_SIZE_BYTES + 1; // Expect $this->validatorMock->expects($this->once())