Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Commit

Permalink
Less method calls on path instantiation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Jul 4, 2014
1 parent fcc1a88 commit 6d43c2b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 76 deletions.
30 changes: 6 additions & 24 deletions src/AbstractPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -978,33 +978,15 @@ public function normalize()
*/
protected function normalizeAtoms($atoms)
{
$normalizedAtoms = array();
foreach ($atoms as $atom) {
$this->validateAtom($atom);
$normalizedAtoms[] = $atom;
if ('' === $atom) {
throw new Exception\EmptyPathAtomException;
} elseif (false !== strpos($atom, static::ATOM_SEPARATOR)) {
throw new Exception\PathAtomContainsSeparatorException($atom);
}
}

return $normalizedAtoms;
}

/**
* Validates a single path atom.
*
* This method is called internally by the constructor upon instantiation.
* It can be overridden in child classes to change how path atoms are
* validated.
*
* @param string $atom The atom to validate.
*
* @throws Exception\InvalidPathAtomExceptionInterface If an invalid path atom is encountered.
*/
protected function validateAtom($atom)
{
if ('' === $atom) {
throw new Exception\EmptyPathAtomException;
} elseif (false !== strpos($atom, static::ATOM_SEPARATOR)) {
throw new Exception\PathAtomContainsSeparatorException($atom);
}
return $atoms;
}

/**
Expand Down
48 changes: 30 additions & 18 deletions src/Windows/AbsoluteWindowsPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Eloquent\Pathogen\AbsolutePath;
use Eloquent\Pathogen\AbsolutePathInterface;
use Eloquent\Pathogen\Exception\EmptyPathAtomException;
use Eloquent\Pathogen\Exception\EmptyPathException;
use Eloquent\Pathogen\Exception\InvalidPathAtomCharacterException;
use Eloquent\Pathogen\Exception\InvalidPathAtomExceptionInterface;
Expand All @@ -22,6 +23,8 @@
use Eloquent\Pathogen\PathInterface;
use Eloquent\Pathogen\RelativePathInterface;
use Eloquent\Pathogen\Resolver\BasePathResolverInterface;
use Eloquent\Pathogen\Windows\Exception\DriveMismatchException;
use Eloquent\Pathogen\Windows\Exception\InvalidDriveSpecifierException;

/**
* Represents an absolute Windows path.
Expand Down Expand Up @@ -62,8 +65,8 @@ public static function fromDriveAndAtoms(
* @param mixed<string> $atoms The path atoms.
* @param boolean|null $hasTrailingSeparator True if this path has a trailing separator.
*
* @throws Exception\InvalidDriveSpecifierException If the drive specifier is invalid.
* @throws InvalidPathAtomExceptionInterface If any of the supplied path atoms are invalid.
* @throws InvalidDriveSpecifierException If the drive specifier is invalid.
* @throws InvalidPathAtomExceptionInterface If any of the supplied path atoms are invalid.
*/
public function __construct($drive, $atoms, $hasTrailingSeparator = null)
{
Expand Down Expand Up @@ -235,14 +238,14 @@ public function string()
*
* @param RelativePathInterface $path The path whose atoms should be joined to this path.
*
* @return PathInterface A new path with the supplied path suffixed to this path.
* @throws Exception\DriveMismatchException If the supplied path has a drive that does not match this path's drive.
* @return PathInterface A new path with the supplied path suffixed to this path.
* @throws DriveMismatchException If the supplied path has a drive that does not match this path's drive.
*/
public function join(RelativePathInterface $path)
{
if ($path instanceof RelativeWindowsPathInterface) {
if (!$this->matchesDriveOrNull($this->pathDriveSpecifier($path))) {
throw new Exception\DriveMismatchException(
throw new DriveMismatchException(
$this->drive(),
$path->drive()
);
Expand Down Expand Up @@ -279,38 +282,47 @@ public function toRelative()
// Implementation details ==================================================

/**
* Validates a single path atom.
* Normalizes and validates a sequence of path atoms.
*
* This method is called internally by the constructor upon instantiation.
* It can be overridden in child classes to change how path atoms are
* validated.
* normalized and/or validated.
*
* @param string $atom The atom to validate.
* @param mixed<string> $atoms The path atoms to normalize.
*
* @throws InvalidPathAtomExceptionInterface If an invalid path atom is encountered.
* @return array<string> The normalized path atoms.
* @throws EmptyPathAtomException If any path atom is empty.
* @throws PathAtomContainsSeparatorException If any path atom contains a separator.
*/
protected function validateAtom($atom)
protected function normalizeAtoms($atoms)
{
parent::validateAtom($atom);

if (false !== strpos($atom, '\\')) {
throw new PathAtomContainsSeparatorException($atom);
} elseif (preg_match('/([\x00-\x1F<>:"|?*])/', $atom, $matches)) {
throw new InvalidPathAtomCharacterException($atom, $matches[1]);
foreach ($atoms as $atom) {
if ('' === $atom) {
throw new EmptyPathAtomException;
} elseif (
false !== strpos($atom, static::ATOM_SEPARATOR) ||
false !== strpos($atom, '\\')
) {
throw new PathAtomContainsSeparatorException($atom);
} elseif (preg_match('/([\x00-\x1F<>:"|?*])/', $atom, $matches)) {
throw new InvalidPathAtomCharacterException($atom, $matches[1]);
}
}

return $atoms;
}

/**
* Validates the suppled drive specifier.
*
* @param string $drive The drive specifier to validate.
*
* @throws Exception\InvalidDriveSpecifierException If the drive specifier is invalid.
* @throws InvalidDriveSpecifierException If the drive specifier is invalid.
*/
protected function validateDriveSpecifier($drive)
{
if (!preg_match('{^[a-zA-Z]$}', $drive)) {
throw new Exception\InvalidDriveSpecifierException($drive);
throw new InvalidDriveSpecifierException($drive);
}
}

Expand Down
59 changes: 25 additions & 34 deletions src/Windows/RelativeWindowsPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@
use Eloquent\Pathogen\Exception\InvalidPathStateException;
use Eloquent\Pathogen\Exception\PathAtomContainsSeparatorException;
use Eloquent\Pathogen\FileSystem\RelativeFileSystemPathInterface;
use Eloquent\Pathogen\Normalizer\PathNormalizerInterface;
use Eloquent\Pathogen\PathInterface;
use Eloquent\Pathogen\RelativePath;
use Eloquent\Pathogen\RelativePathInterface;
use Eloquent\Pathogen\Windows\Exception\DriveMismatchException;
use Eloquent\Pathogen\Windows\Exception\InvalidDriveSpecifierException;
use Eloquent\Pathogen\Windows\Factory\WindowsPathFactory;
use Eloquent\Pathogen\Windows\Factory\WindowsPathFactoryInterface;
use Eloquent\Pathogen\Windows\Normalizer\WindowsPathNormalizer;

/**
* Represents a relative Windows path.
Expand Down Expand Up @@ -237,14 +243,14 @@ public function string()
*
* @param RelativePathInterface $path The path whose atoms should be joined to this path.
*
* @return PathInterface A new path with the supplied path suffixed to this path.
* @throws Exception\DriveMismatchException If the supplied path has a drive that does not match this path's drive.
* @return PathInterface A new path with the supplied path suffixed to this path.
* @throws DriveMismatchException If the supplied path has a drive that does not match this path's drive.
*/
public function join(RelativePathInterface $path)
{
if ($path instanceof RelativeWindowsPathInterface) {
if (!$this->matchesDriveOrNull($this->pathDriveSpecifier($path))) {
throw new Exception\DriveMismatchException(
throw new DriveMismatchException(
$this->drive(),
$path->drive()
);
Expand Down Expand Up @@ -302,48 +308,33 @@ public function toAbsolute()
*/
protected function normalizeAtoms($atoms)
{
$normalizedAtoms = array();
foreach ($atoms as $atom) {
$this->validateAtom($atom);
$normalizedAtoms[] = $atom;
if ('' === $atom) {
throw new EmptyPathAtomException;
} elseif (
false !== strpos($atom, static::ATOM_SEPARATOR) ||
false !== strpos($atom, '\\')
) {
throw new PathAtomContainsSeparatorException($atom);
} elseif (preg_match('/([\x00-\x1F<>:"|?*])/', $atom, $matches)) {
throw new InvalidPathAtomCharacterException($atom, $matches[1]);
}
}

return $normalizedAtoms;
}

/**
* Validates a single path atom.
*
* This method is called internally by the constructor upon instantiation.
* It can be overridden in child classes to change how path atoms are
* validated.
*
* @param string $atom The atom to validate.
*
* @throws InvalidPathAtomExceptionInterface If an invalid path atom is encountered.
*/
protected function validateAtom($atom)
{
parent::validateAtom($atom);

if (false !== strpos($atom, '\\')) {
throw new PathAtomContainsSeparatorException($atom);
} elseif (preg_match('/([\x00-\x1F<>:"|?*])/', $atom, $matches)) {
throw new InvalidPathAtomCharacterException($atom, $matches[1]);
}
return $atoms;
}

/**
* Validates the suppled drive specifier.
*
* @param string $drive The drive specifier to validate.
*
* @throws Exception\InvalidDriveSpecifierException If the drive specifier is invalid.
* @throws InvalidDriveSpecifierException If the drive specifier is invalid.
*/
protected function validateDriveSpecifier($drive)
{
if (!preg_match('{^[a-zA-Z]$}', $drive)) {
throw new Exception\InvalidDriveSpecifierException($drive);
throw new InvalidDriveSpecifierException($drive);
}
}

Expand Down Expand Up @@ -464,11 +455,11 @@ protected function createPathFromDriveAndAtoms(
/**
* Get the most appropriate path factory for this type of path.
*
* @return Factory\WindowsPathFactoryInterface The path factory.
* @return WindowsPathFactoryInterface The path factory.
*/
protected static function factory()
{
return Factory\WindowsPathFactory::instance();
return WindowsPathFactory::instance();
}

/**
Expand All @@ -478,7 +469,7 @@ protected static function factory()
*/
protected static function normalizer()
{
return Normalizer\WindowsPathNormalizer::instance();
return WindowsPathNormalizer::instance();
}

private $drive;
Expand Down

0 comments on commit 6d43c2b

Please sign in to comment.