| Total Complexity | 6 |
| Total Lines | 45 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 15 | class FindFreeName |
||
| 16 | { |
||
| 17 | const FREE_NAME_SEPARATOR = '_'; |
||
| 18 | |||
| 19 | /** @var Interfaces\IProcessNodes */ |
||
| 20 | protected $nodes = null; |
||
| 21 | |||
| 22 | 10 | public function __construct(Interfaces\IProcessNodes $nodes) |
|
| 23 | { |
||
| 24 | 10 | $this->nodes = $nodes; |
|
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param string[] $path |
||
| 29 | * @param string $name |
||
| 30 | * @param string $suffix |
||
| 31 | * @throws FilesException |
||
| 32 | * @return string |
||
| 33 | */ |
||
| 34 | 10 | public function findFreeName(array $path, string $name, string $suffix): string |
|
| 35 | { |
||
| 36 | 10 | if (!$this->targetExists($path, $name . $suffix)) { |
|
| 37 | 4 | return $name . $suffix; |
|
| 38 | } |
||
| 39 | 6 | $i = 0; |
|
| 40 | 6 | while ($this->targetExists($path, $name . $this->getNameSeparator() . strval($i) . $suffix)) { |
|
| 41 | 2 | $i++; |
|
| 42 | } |
||
| 43 | 6 | return $name . $this->getNameSeparator() . strval($i) . $suffix; |
|
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param array<string> $path |
||
| 48 | * @param string $name |
||
| 49 | * @throws FilesException |
||
| 50 | * @return bool |
||
| 51 | */ |
||
| 52 | 10 | protected function targetExists(array $path, string $name): bool |
|
| 55 | } |
||
| 56 | |||
| 57 | 3 | protected function getNameSeparator(): string |
|
| 60 | } |
||
| 61 | } |
||
| 62 |