src/Path.php
public static function getLongestCommonBasePath(array $paths)
    {
        Assert::allString($paths, 'The paths must be strings. Got: %s');

        list($bpRoot, $basePath) = self::split(self::canonicalize(reset($paths)));

        for (next($paths); null !== key($paths) && '' !== $basePath; next($paths)) {
            list($root, $path) = self::split(self::canonicalize(current($paths)));

            // If we deal with different roots (e.g. C:/ vs. D:/), it's time
            // to quit
            if ($root !== $bpRoot) {
                return null;
            }

            // Make the base path shorter until it fits into path
            while (true) {
                if ('.' === $basePath) {
                    // No more base paths
                    $basePath = '';

                    // Next path
                    continue 2;
                }

                // Prevent false positives for common prefixes
                // see isBasePath()
                if (0 === strpos($path.'/', $basePath.'/')) {
                    // Next path
                    continue 2;
                }

                $basePath = dirname($basePath);
            }
        }

        return $bpRoot.$basePath;
    }
public static function getLongestCommonBasePath(array $paths)
    {
        Assert::allString($paths, 'The paths must be strings. Got: %s');

        list($bpRoot, $basePath) = self::split(self::canonicalize(reset($paths)));

        for (next($paths); null !== key($paths) && '' !== $basePath; next($paths)) {
            list($root, $path) = self::split(self::canonicalize(current($paths)));

            // If we deal with different roots (e.g. C:/ vs. D:/), it's time
            // to quit
            if ($root !== $bpRoot) {
                return null;
            }

            // Make the base path shorter until it fits into path
            while (true) {
                if ('.' === $basePath) {
                    // No more base paths
                    $basePath = '';

                    // Next path
                    continue 2;
                }

                // Prevent false positives for common prefixes
                // see isBasePath()
                if (0 === strpos($path.'/', $basePath.'/')) {
                    // Next path
                    continue 2;
                }

                $basePath = dirname($basePath);
            }
        }

        return $bpRoot.$basePath;
    }
public static function getLongestCommonBasePath ( array $ paths ) { Assert :: allString ( $ paths , 'The paths must be strings. Got: %s' ) ; list ( $ bpRoot , $ basePath ) = self :: split ( self :: canonicalize ( reset ( $ paths ) ) ) ; for ( next ( $ paths ) ; null !== key ( $ paths ) && '' !== $ basePath ; next ( $ paths ) ) { list ( $ root , $ path ) = self :: split ( self :: canonicalize ( current ( $ paths ) ) ) ; // If we deal with different roots (e.g. C:/ vs. D:/), it's time // to quit if ( $ root !== $ bpRoot ) { return null ; } // Make the base path shorter until it fits into path while ( true ) { if ( '.' === $ basePath ) { // No more base paths $ basePath = '' ; // Next path continue 2 ; } // Prevent false positives for common prefixes // see isBasePath() if ( 0 === strpos ( $ path . '/' , $ basePath . '/' ) ) { // Next path continue 2 ; } $ basePath = dirname ( $ basePath ) ; } } return $ bpRoot . $ basePath ; }
Returns the longest common base path of a set of paths.

Dot segments ("." and "..") are removed/collapsed and all slashes turned
into forward slashes.

```php
$basePath = Path::getLongestCommonBasePath(array(
'/webmozart/css/style.css',
'/webmozart/css/..'
));
// => /webmozart
```

The root is returned if no common base path can be found:

```php
$basePath = Path::getLongestCommonBasePath(array(
'/webmozart/css/style.css',
'/puli/css/..'
));
// => /
```

If the paths are located on different Windows partitions, `null` is
returned.

```php
$basePath = Path::getLongestCommonBasePath(array(
'C:/webmozart/css/style.css',
'D:/webmozart/css/..'
));
// => null
```

@param array $paths A list of paths.

@return string|null The longest common base path in canonical form or
`null` if the paths are on different Windows
partitions.

@since 1.0 Added method.
@since 2.0 Method now fails if $paths are not strings.
Returns the longest common base path of a set of paths .