Skip to content

Commit 3da7fb2

Browse files
committed
feat: Upgrade PHPStan level to 5 for stricter type checking
feat: Enhance Generator contract with additional methods for better configuration and generation control feat: Update Presenter contract to enforce type safety in formatting method feat: Improve exception handling with detailed messages for invalid running number types and max number limits feat: Implement ConfigurationException and NumberGenerationException for better error management test: Add comprehensive tests for edge cases, concurrency, and configuration validation in RunningNumber generation
1 parent 6257425 commit 3da7fb2

16 files changed

+1278
-79
lines changed

docs/06-upgrade-guide.md

Lines changed: 426 additions & 0 deletions
Large diffs are not rendered by default.

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ includes:
22
- phpstan-baseline.neon
33

44
parameters:
5-
level: 4
5+
level: 5
66
paths:
77
- src
88
- config

src/Contracts/Generator.php

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,87 @@
22

33
namespace CleaniqueCoders\RunningNumber\Contracts;
44

5+
/**
6+
* Contract for running number generators
7+
*/
58
interface Generator
69
{
7-
public static function make(): Generator;
10+
/**
11+
* Create a new generator instance
12+
*/
13+
public static function make(): self;
814

9-
public function formatter(Presenter $presenter): Generator;
15+
/**
16+
* Set a custom formatter for the running number
17+
*
18+
* @param Presenter $presenter The presenter to format the output
19+
*/
20+
public function formatter(Presenter $presenter): self;
1021

22+
/**
23+
* Set the type of running number to generate
24+
*
25+
* @param string $type The running number type
26+
*/
27+
public function type(string $type): self;
28+
29+
/**
30+
* Set the scope for the running number sequence
31+
*
32+
* @param string|null $scope The scope identifier
33+
*/
34+
public function scope(?string $scope): self;
35+
36+
/**
37+
* Set the starting number for new sequences
38+
*
39+
* @param int $number The starting number
40+
*/
41+
public function startFrom(int $number): self;
42+
43+
/**
44+
* Set the maximum number limit
45+
*
46+
* @param int $number The maximum number
47+
*/
48+
public function maxNumber(int $number): self;
49+
50+
/**
51+
* Control case transformation of the type prefix
52+
*
53+
* @param bool $value Whether to convert to uppercase
54+
*/
55+
public function toUpperCase(bool $value): self;
56+
57+
/**
58+
* Generate the next running number
59+
*
60+
* @return string The formatted running number
61+
*
62+
* @throws \CleaniqueCoders\RunningNumber\Exceptions\InvalidRunningNumberTypeException
63+
* @throws \CleaniqueCoders\RunningNumber\Exceptions\MaxNumberReachedException
64+
* @throws \CleaniqueCoders\RunningNumber\Exceptions\NumberGenerationException
65+
*/
1166
public function generate(): string;
67+
68+
/**
69+
* Preview the next running number without incrementing
70+
*
71+
* @return string The formatted preview
72+
*
73+
* @throws \CleaniqueCoders\RunningNumber\Exceptions\InvalidRunningNumberTypeException
74+
*/
75+
public function preview(): string;
76+
77+
/**
78+
* Generate multiple running numbers at once
79+
*
80+
* @param int $count The number of running numbers to generate
81+
* @return array<int, string> Array of formatted running numbers
82+
*
83+
* @throws \CleaniqueCoders\RunningNumber\Exceptions\InvalidRunningNumberTypeException
84+
* @throws \CleaniqueCoders\RunningNumber\Exceptions\MaxNumberReachedException
85+
* @throws \CleaniqueCoders\RunningNumber\Exceptions\NumberGenerationException
86+
*/
87+
public function generateBatch(int $count): array;
1288
}

src/Contracts/Presenter.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
namespace CleaniqueCoders\RunningNumber\Contracts;
44

5+
/**
6+
* Contract for running number formatters/presenters
7+
*/
58
interface Presenter
69
{
7-
public function format($type, $number): string;
10+
/**
11+
* Format the running number
12+
*
13+
* @param string $type The running number type/prefix
14+
* @param int $number The sequence number
15+
* @return string The formatted running number
16+
*/
17+
public function format(string $type, int $number): string;
818
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace CleaniqueCoders\RunningNumber\Exceptions;
4+
5+
use Exception;
6+
7+
/**
8+
* Exception thrown when configuration is invalid
9+
*/
10+
class ConfigurationException extends Exception
11+
{
12+
/**
13+
* Create a new ConfigurationException instance
14+
*
15+
* @param string $message The error message
16+
* @param int $code The error code
17+
* @param \Throwable|null $previous The previous exception
18+
*/
19+
public function __construct(string $message = 'Invalid configuration', int $code = 0, ?\Throwable $previous = null)
20+
{
21+
parent::__construct($message, $code, $previous);
22+
}
23+
}

src/Exceptions/InvalidRunningNumberTypeException.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,23 @@
44

55
use Exception;
66

7+
/**
8+
* Exception thrown when an invalid or unsupported running number type is used
9+
*
10+
* This occurs when attempting to generate a running number with a type
11+
* that is not configured in the running-number.types array.
12+
*/
713
class InvalidRunningNumberTypeException extends Exception
814
{
9-
//
15+
/**
16+
* Create a new InvalidRunningNumberTypeException instance
17+
*
18+
* @param string $message The error message
19+
* @param int $code The error code
20+
* @param \Throwable|null $previous The previous exception
21+
*/
22+
public function __construct(string $message = 'Invalid running number type', int $code = 0, ?\Throwable $previous = null)
23+
{
24+
parent::__construct($message, $code, $previous);
25+
}
1026
}

src/Exceptions/MaxNumberReachedException.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,24 @@
44

55
use Exception;
66

7+
/**
8+
* Exception thrown when the maximum number limit is reached
9+
*
10+
* This occurs when attempting to generate a number that would exceed
11+
* the configured maximum limit for a running number type.
12+
*/
713
class MaxNumberReachedException extends Exception
814
{
15+
/**
16+
* Create a new MaxNumberReachedException instance
17+
*
18+
* @param string $type The running number type
19+
* @param int $maxNumber The maximum number limit
20+
*/
921
public function __construct(string $type, int $maxNumber)
1022
{
11-
parent::__construct("Maximum number {$maxNumber} reached for type {$type}");
23+
parent::__construct(
24+
sprintf('Maximum number %d reached for running number type "%s"', $maxNumber, $type)
25+
);
1226
}
1327
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace CleaniqueCoders\RunningNumber\Exceptions;
4+
5+
use Exception;
6+
7+
/**
8+
* Exception thrown when number generation fails
9+
*/
10+
class NumberGenerationException extends Exception
11+
{
12+
/**
13+
* Create a new NumberGenerationException instance
14+
*
15+
* @param string $message The error message
16+
* @param int $code The error code
17+
* @param \Throwable|null $previous The previous exception
18+
*/
19+
public function __construct(string $message = 'Failed to generate running number', int $code = 0, ?\Throwable $previous = null)
20+
{
21+
parent::__construct($message, $code, $previous);
22+
}
23+
}

0 commit comments

Comments
 (0)