for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Druc\Flysystem\IncrementalNaming;
trait IncrementsPath
{
/**
* @param $filePath
* @return string
*/
private function getIncrementedPath($filePath)
$originalFilePath = $filePath;
$increment = 1;
while ($this->fileExists($filePath)) {
$filePath = $this->incrementPath($originalFilePath, $increment);
$increment++;
}
return $filePath;
* @return bool
private function fileExists($filePath)
$directory = pathinfo($filePath, PATHINFO_DIRNAME);
$existingPaths = array_column($this->filesystem->listContents($directory), 'path');
filesystem
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
return in_array($filePath, $existingPaths);
* @param $originalFilePath
* @param $increment
* @return null|string|string[]
private function incrementPath($originalFilePath, $increment)
if ($this->pathHasExtension($originalFilePath)) {
return preg_replace('#^(.+)\.([\w]+)$#i', '$1_' . $increment . '.$2', $originalFilePath);
return $originalFilePath . '_' . $increment;
* @return bool|int
private function pathHasExtension($filePath)
return (bool)strpos($filePath, '.');
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: