From 69d7b8653400d9652ad656424758699e0f6be101 Mon Sep 17 00:00:00 2001 From: Netkas Date: Mon, 21 Aug 2023 23:14:13 -0400 Subject: [PATCH] - Removed unused exception `PackageNotFoundException` in `\ncc\Exceptions` in favor of `PackageException` - Removed unused exception `ConstantReadonlyException` in `\ncc\Exceptions` in favor of `IntegrityException` & `\InvalidArgumentException` - Removed unused exception `InvalidConstantName` in `\ncc\Exceptions` in favor for `IntegrityException` - Removed unused exception `ComponentDecodeException` in `\ncc\Exceptions` in favor for `IntegrityException` - Removed unused exception `ResourceChecksumException` in `\ncc\Exceptions` in favor for `IntegrityException` - Removed unused exception `AutoloadGeneratorException` in `\ncc\Exceptions` in favor for `IOException` - Removed unused exception `SymlinkException` in `\ncc\Exceptions` in favor for `IOException` - Removed unused exception `PackageLockException` in `\ncc\Exceptions` in favor for `IOException` --- CHANGELOG.md | 9 ++ scratch/exceptions_plan.txt | 26 +++--- src/ncc/CLI/Management/PackageManagerMenu.php | 16 ++-- src/ncc/CLI/Management/ProjectMenu.php | 6 -- .../ComposerSourceBuiltin.php | 6 +- src/ncc/Classes/PhpExtension/PhpCompiler.php | 9 +- src/ncc/Classes/PhpExtension/PhpInstaller.php | 18 ++-- src/ncc/Classes/PhpExtension/PhpRuntime.php | 66 +++++++------- src/ncc/Enums/ExceptionCodes.php | 50 ++-------- .../Exceptions/AutoloadGeneratorException.php | 39 -------- .../Exceptions/ComponentDecodeException.php | 39 -------- .../ComposerNotAvailableException.php | 38 -------- .../Exceptions/ConstantReadonlyException.php | 40 -------- src/ncc/Exceptions/IntegrityException.php | 34 +++++++ .../InvalidConstantNameException.php | 39 -------- src/ncc/Exceptions/PackageLockException.php | 39 -------- .../Exceptions/PackageNotFoundException.php | 39 -------- .../ProjectAlreadyExistsException.php | 40 -------- .../Exceptions/ResourceChecksumException.php | 39 -------- src/ncc/Exceptions/SymlinkException.php | 39 -------- src/ncc/Interfaces/InstallerInterface.php | 2 - src/ncc/Interfaces/RuntimeInterface.php | 50 +++++----- src/ncc/Managers/PackageLockManager.php | 71 ++++++++------- src/ncc/Managers/PackageManager.php | 47 +++------- src/ncc/Managers/ProjectManager.php | 5 +- src/ncc/Managers/SymlinkManager.php | 21 +++-- src/ncc/Objects/Constant.php | 91 ++++++++++--------- src/ncc/Objects/PackageLock/PackageEntry.php | 48 +++++----- src/ncc/Runtime.php | 29 +++--- src/ncc/Runtime/Constants.php | 21 +++-- 30 files changed, 299 insertions(+), 717 deletions(-) delete mode 100644 src/ncc/Exceptions/AutoloadGeneratorException.php delete mode 100644 src/ncc/Exceptions/ComponentDecodeException.php delete mode 100644 src/ncc/Exceptions/ComposerNotAvailableException.php delete mode 100644 src/ncc/Exceptions/ConstantReadonlyException.php create mode 100644 src/ncc/Exceptions/IntegrityException.php delete mode 100644 src/ncc/Exceptions/InvalidConstantNameException.php delete mode 100644 src/ncc/Exceptions/PackageLockException.php delete mode 100644 src/ncc/Exceptions/PackageNotFoundException.php delete mode 100644 src/ncc/Exceptions/ProjectAlreadyExistsException.php delete mode 100644 src/ncc/Exceptions/ResourceChecksumException.php delete mode 100644 src/ncc/Exceptions/SymlinkException.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ea8bc7..f042b5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -112,6 +112,7 @@ features. - Updated `\ncc\Classes > GitClient > cloneRepository()` to throw `GitException` instead of `GitCloneException` - Updated `\ncc\Classes > GitClient > checkout()` to throw `GitException` instead of `GitCheckoutException` - Corrected code-smell and code style issues in `\ncc\Objects > PackageLock` + - Corrected code-smell and code style issues in `\ncc\Classes\PhpExtension > PhpRuntime` ### Removed - Removed `FileNotFoundException` and `DirectoryNotFoundException` from `\ncc\Exceptions` @@ -158,6 +159,14 @@ features. - Removed unused exception `MissingDependencyException` in `\ncc\Exceptions` (not used) - Removed unused exception `PackageAlreadyInstalledException` in `\ncc\Exceptions` in favor of `PackageException` - Removed unused exception `PackageFetchException` in `\ncc\Exceptions` in favor of `PackageException` + - Removed unused exception `PackageNotFoundException` in `\ncc\Exceptions` in favor of `PackageException` + - Removed unused exception `ConstantReadonlyException` in `\ncc\Exceptions` in favor of `IntegrityException` & `\InvalidArgumentException` + - Removed unused exception `InvalidConstantName` in `\ncc\Exceptions` in favor for `IntegrityException` + - Removed unused exception `ComponentDecodeException` in `\ncc\Exceptions` in favor for `IntegrityException` + - Removed unused exception `ResourceChecksumException` in `\ncc\Exceptions` in favor for `IntegrityException` + - Removed unused exception `AutoloadGeneratorException` in `\ncc\Exceptions` in favor for `IOException` + - Removed unused exception `SymlinkException` in `\ncc\Exceptions` in favor for `IOException` + - Removed unused exception `PackageLockException` in `\ncc\Exceptions` in favor for `IOException` diff --git a/scratch/exceptions_plan.txt b/scratch/exceptions_plan.txt index 3ddd487..8c255c5 100644 --- a/scratch/exceptions_plan.txt +++ b/scratch/exceptions_plan.txt @@ -24,24 +24,22 @@ AuthenticationException ResourceNotFoundException PROJECT_ALREADY_EXISTS NO_UNITS_FOUND - PACKAGE_NOT_FOUND VERSION_NOT_FOUND NO_AVAILABLE_UNITS -PackageLockException - PACKAGE_LOCK_EXCEPTION - - PackageException * PACKAGE_PREPARATION_FAILED * PACKAGE_PARSING_EXCEPTION - PACKAGE_ALREADY_INSTALLED - PACKAGE_FETCH_EXCEPTION + * PACKAGE_ALREADY_INSTALLED + * PACKAGE_FETCH_EXCEPTION - MISSING_DEPENDENCY + * PACKAGE_NOT_FOUND IOException - IO_EXCEPTION - SYMLINK_EXCEPTION + * IO_EXCEPTION + * AUTOLOADER_GENERATOR_EXCEPTION + * SYMLINK_EXCEPTION + * PACKAGE_LOCK_EXCEPTION NetworkException * HTTP_EXCEPTION @@ -80,10 +78,10 @@ RuntimeException USER_ABORTED_OPERATION IntegrityException - CONSTANT_READ_ONLY - INVALID_CONSTANT_NAME - COMPONENT_DECODE_EXCEPTION - RESOURCE_CHECKSUM_EXCEPTION + * CONSTANT_READ_ONLY + * INVALID_CONSTANT_NAME + * COMPONENT_DECODE_EXCEPTION + * RESOURCE_CHECKSUM_EXCEPTION AutoLoadException - AUTOLOAD_GENERATOR \ No newline at end of file + - AUTOLOAD_GENERATOR \ No newline at end of file diff --git a/src/ncc/CLI/Management/PackageManagerMenu.php b/src/ncc/CLI/Management/PackageManagerMenu.php index 37d00c1..bda5ed2 100644 --- a/src/ncc/CLI/Management/PackageManagerMenu.php +++ b/src/ncc/CLI/Management/PackageManagerMenu.php @@ -27,9 +27,7 @@ use ncc\Enums\ConsoleColors; use ncc\Enums\Options\InstallPackageOptions; use ncc\Enums\Scopes; - use ncc\Exceptions\PackageLockException; - use ncc\Exceptions\RuntimeException; - use ncc\Exceptions\VersionNotFoundException; + use ncc\Exceptions\IOException; use ncc\Managers\CredentialManager; use ncc\Managers\PackageManager; use ncc\Objects\CliHelpSection; @@ -376,7 +374,7 @@ { $credential->unlock(Console::passwordInput(sprintf('Enter Password for %s: ', $credential->getName()))); } - catch (RuntimeException $e) + catch (Exception $e) { Console::outException(sprintf('Failed to unlock credential %s', $credential->getName()), $e, 1); return; @@ -507,7 +505,7 @@ { $dependency_package = $package_manager->getPackage($dependency->name); } - catch (PackageLockException $e) + catch (IOException $e) { unset($e); $dependency_package = null; @@ -519,7 +517,7 @@ { $dependency_version = $dependency_package->getVersion($dependency->version); } - catch (VersionNotFoundException $e) + catch (IOException $e) { unset($e); $dependency_version = null; @@ -596,7 +594,7 @@ * * @param $args * @return void - * @throws VersionNotFoundException + * @throws IOException */ private static function uninstallPackage($args): void { @@ -622,7 +620,7 @@ { $package_entry = $package_manager->getPackage($selected_package); } - catch (PackageLockException $e) + catch (IOException $e) { Console::outException('PackageLock error', $e, 1); return; @@ -688,7 +686,7 @@ * * @param $args * @return void - * @throws PackageLockException + * @throws IOException */ private static function uninstallAllPackages($args): void { diff --git a/src/ncc/CLI/Management/ProjectMenu.php b/src/ncc/CLI/Management/ProjectMenu.php index 206f836..d08961b 100644 --- a/src/ncc/CLI/Management/ProjectMenu.php +++ b/src/ncc/CLI/Management/ProjectMenu.php @@ -30,7 +30,6 @@ use ncc\Exceptions\IOException; use ncc\Exceptions\MalformedJsonException; use ncc\Exceptions\PathNotFoundException; - use ncc\Exceptions\ProjectAlreadyExistsException; use ncc\Managers\ProjectManager; use ncc\Objects\CliHelpSection; use ncc\Objects\ProjectConfiguration\Compiler; @@ -217,11 +216,6 @@ Console::outException(sprintf('The project configuration is invalid: %s', $e->getMessage()), $e, 1); return; } - catch (ProjectAlreadyExistsException $e) - { - Console::outException('A project has already been initialized in \'' . $current_directory . '\'', $e, 1); - return; - } catch(Exception $e) { Console::outException('There was an unexpected error while trying to initialize the project', $e, 1); diff --git a/src/ncc/Classes/ComposerExtension/ComposerSourceBuiltin.php b/src/ncc/Classes/ComposerExtension/ComposerSourceBuiltin.php index 1dbabd8..18e9292 100644 --- a/src/ncc/Classes/ComposerExtension/ComposerSourceBuiltin.php +++ b/src/ncc/Classes/ComposerExtension/ComposerSourceBuiltin.php @@ -40,7 +40,6 @@ use ncc\Exceptions\PathNotFoundException; use ncc\Exceptions\IOException; use ncc\Exceptions\MalformedJsonException; - use ncc\Exceptions\PackageNotFoundException; use ncc\Exceptions\RuntimeException; use ncc\Exceptions\UserAbortedOperationException; use ncc\Interfaces\ServiceSourceInterface; @@ -82,7 +81,6 @@ * @throws MalformedJsonException * @throws NotSupportedException * @throws PackageException - * @throws PackageNotFoundException * @throws PathNotFoundException * @throws RuntimeException * @throws UserAbortedOperationException @@ -118,7 +116,6 @@ * @throws MalformedJsonException * @throws NotSupportedException * @throws PackageException - * @throws PackageNotFoundException * @throws PathNotFoundException * @throws UserAbortedOperationException */ @@ -188,7 +185,6 @@ * @throws MalformedJsonException * @throws NotSupportedException * @throws PackageException - * @throws PackageNotFoundException * @throws PathNotFoundException */ private static function compilePackages(string $composer_lock_path): array @@ -228,7 +224,7 @@ $composer_package = $composer_lock->getPackage($package->Name); if ($composer_package === null) { - throw new PackageNotFoundException(sprintf('Package "%s" not found in composer lock file', $package->Name)); + throw new PackageException(sprintf('Package "%s" not found in composer lock file', $package->Name)); } // Convert it to an NCC project configuration diff --git a/src/ncc/Classes/PhpExtension/PhpCompiler.php b/src/ncc/Classes/PhpExtension/PhpCompiler.php index a735979..a46fc85 100644 --- a/src/ncc/Classes/PhpExtension/PhpCompiler.php +++ b/src/ncc/Classes/PhpExtension/PhpCompiler.php @@ -36,7 +36,6 @@ use ncc\Exceptions\ConfigurationException; use ncc\Exceptions\IOException; use ncc\Exceptions\PackageException; - use ncc\Exceptions\PackageLockException; use ncc\Exceptions\PathNotFoundException; use ncc\Exceptions\RunnerExecutionException; use ncc\Exceptions\VersionNotFoundException; @@ -274,7 +273,7 @@ $package = $package_lock_manager->getPackageLock()?->getPackage($dependency->Name); if($package === null) { - throw new PackageLockException('Cannot find package lock for dependency ' . $dependency->Name); + throw new IOException('Cannot find package lock for dependency ' . $dependency->Name); } $version = $package->getVersion($dependency->Version); @@ -294,11 +293,7 @@ $dependency->Source = 'libs' . DIRECTORY_SEPARATOR . sprintf('%s=%s.lib', $dependency->Name, $dependency->Version); } - catch (VersionNotFoundException $e) - { - throw new PackageException('Static linking not possible, cannot find version ' . $dependency->Version . ' for dependency ' . $dependency->Name, $e); - } - catch (PackageLockException $e) + catch (IOException $e) { throw new PackageException('Static linking not possible, cannot find package lock for dependency ' . $dependency->Name, $e); } diff --git a/src/ncc/Classes/PhpExtension/PhpInstaller.php b/src/ncc/Classes/PhpExtension/PhpInstaller.php index b65847b..665c19e 100644 --- a/src/ncc/Classes/PhpExtension/PhpInstaller.php +++ b/src/ncc/Classes/PhpExtension/PhpInstaller.php @@ -29,16 +29,15 @@ use Exception; use ncc\Enums\ComponentDataType; use ncc\Enums\ComponentFileExtensions; - use ncc\Exceptions\ComponentChecksumException; - use ncc\Exceptions\ComponentDecodeException; + use ncc\Exceptions\IntegrityException; use ncc\Exceptions\IOException; use ncc\Exceptions\NotSupportedException; use ncc\Exceptions\PathNotFoundException; - use ncc\Exceptions\ResourceChecksumException; use ncc\Interfaces\InstallerInterface; use ncc\Objects\InstallationPaths; use ncc\Objects\Package; use ncc\Objects\Package\Component; + use ncc\Objects\Package\Resource; use ncc\ThirdParty\nikic\PhpParser\Comment; use ncc\ThirdParty\nikic\PhpParser\Node; use ncc\ThirdParty\nikic\PhpParser\PrettyPrinter\Standard; @@ -75,8 +74,7 @@ * * @param Component $component * @return string|null - * @throws ComponentChecksumException - * @throws ComponentDecodeException + * @throws IntegrityException * @throws NotSupportedException */ public function processComponent(Package\Component $component): ?string @@ -88,7 +86,7 @@ if(!$component->validate_checksum()) { - throw new ComponentChecksumException('Checksum validation failed for component ' . $component->name . ', the package may be corrupted.'); + throw new IntegrityException(sprintf('Checksum validation failed for component: %s', $component->name)); } switch($component->data_types) @@ -100,7 +98,7 @@ } catch (Exception $e) { - throw new ComponentDecodeException('Cannot decode component: ' . $component->name . ', ' . $e->getMessage(), $e); + throw new IntegrityException(sprintf('Cannot decode component: %s, %s', $component->name, $e->getMessage())); } return (new Standard())->prettyPrintFile($stmts); @@ -137,15 +135,15 @@ /** * Processes the given resource and returns the string representation of the resource * - * @param Package\Resource $resource + * @param Resource $resource * @return string|null - * @throws ResourceChecksumException + * @throws IntegrityException */ public function processResource(Package\Resource $resource): ?string { if(!$resource->validateChecksum()) { - throw new ResourceChecksumException('Checksum validation failed for resource ' . $resource->Name . ', the package may be corrupted.'); + throw new IntegrityException('Checksum validation failed for resource ' . $resource->Name . ', the package may be corrupted.'); } return Base64::decode($resource->Data); diff --git a/src/ncc/Classes/PhpExtension/PhpRuntime.php b/src/ncc/Classes/PhpExtension/PhpRuntime.php index ca95f7d..0a9647a 100644 --- a/src/ncc/Classes/PhpExtension/PhpRuntime.php +++ b/src/ncc/Classes/PhpExtension/PhpRuntime.php @@ -1,33 +1,32 @@ getInstallPaths()->getDataPath() . DIRECTORY_SEPARATOR . 'assembly'; if(!file_exists($assembly_path)) + { throw new ImportException('Cannot locate assembly file \'' . $assembly_path . '\''); + } try { @@ -85,29 +86,27 @@ namespace ncc\Classes\PhpExtension; { Constants::register($assembly->package, $name, $value, true); } - catch (ConstantReadonlyException $e) + catch (IntegrityException $e) { - trigger_error('Constant \'' . $name . '\' is readonly (' . $assembly->package . ')', E_USER_WARNING); - } - catch (InvalidConstantNameException $e) - { - throw new ImportException('Invalid constant name \'' . $name . '\' (' . $assembly->package . ')', $e); + trigger_error('Cannot set constant \'' . $name . '\', ' . $e->getMessage(), E_USER_WARNING); } } } - if(file_exists($autoload_path) && !in_array(RuntimeImportOptions::IMPORT_AUTOLOADER, $options)) + if(file_exists($autoload_path) && !in_array(RuntimeImportOptions::IMPORT_AUTOLOADER, $options, true)) { require_once($autoload_path); } - if(file_exists($static_files) && !in_array(RuntimeImportOptions::IMPORT_STATIC_FILES, $options)) + if(file_exists($static_files) && !in_array(RuntimeImportOptions::IMPORT_STATIC_FILES, $options, true)) { try { $static_files = ZiProto::decode(IO::fread($static_files)); foreach($static_files as $file) + { require_once($file); + } } catch(Exception $e) { @@ -116,9 +115,6 @@ namespace ncc\Classes\PhpExtension; } - if(!file_exists($autoload_path) && !file_exists($static_files)) - return false; - - return true; + return !(!file_exists($autoload_path) && !file_exists($static_files)); } } \ No newline at end of file diff --git a/src/ncc/Enums/ExceptionCodes.php b/src/ncc/Enums/ExceptionCodes.php index f7d1b45..736c7ed 100644 --- a/src/ncc/Enums/ExceptionCodes.php +++ b/src/ncc/Enums/ExceptionCodes.php @@ -43,46 +43,21 @@ */ public const CONSTANT_READ_ONLY = -1709; - /** - * @see ProjectAlreadyExistsException - */ - public const PROJECT_ALREADY_EXISTS = -1713; - - /** - * @see AutoloadGeneratorException - */ - public const AUTOLOAD_GENERATOR = -1714; - /** * @see NoUnitsFoundException */ public const NO_UNITS_FOUND = -1715; - /** - * @see InvalidConstantNameException - */ - public const INVALID_CONSTANT_NAME = -1719; - /** * @see BuildException */ public const BUILD_EXCEPTION = -1727; - /** - * @see PackageLockException - */ - public const PACKAGE_LOCK_EXCEPTION = -1729; - /** * @see InstallationException */ public const INSTALLATION_EXCEPTION = -1730; - /** - * @see ComponentDecodeException - */ - public const COMPONENT_DECODE_EXCEPTION = -1732; - /** * @see IOException */ @@ -103,11 +78,6 @@ */ public const NO_AVAILABLE_UNITS = -1742; - /** - * @see PackageNotFoundException - */ - public const PACKAGE_NOT_FOUND = -1745; - /** * @see ComposerException */ @@ -138,11 +108,6 @@ */ public const ARCHIVE_EXCEPTION = -1764; - /** - * @see SymlinkException - */ - public const SYMLINK_EXCEPTION = -1768; - /** * @see PathNotFoundException */ @@ -168,6 +133,11 @@ */ public const NETWORK_EXCEPTION = -1774; + /** + * @see IntegrityException + */ + public const INTEGRITY_EXCEPTION = -1775; + /** * All the exception codes from NCC */ @@ -175,29 +145,23 @@ self::MALFORMED_JSON, self::RUNTIME, self::CONSTANT_READ_ONLY, - self::PROJECT_ALREADY_EXISTS, - self::AUTOLOAD_GENERATOR, self::NO_UNITS_FOUND, - self::INVALID_CONSTANT_NAME, self::BUILD_EXCEPTION, - self::PACKAGE_LOCK_EXCEPTION, self::INSTALLATION_EXCEPTION, - self::COMPONENT_DECODE_EXCEPTION, self::IO_EXCEPTION, self::VERSION_NOT_FOUND, self::RUNNER_EXECUTION_EXCEPTION, self::NO_AVAILABLE_UNITS, - self::PACKAGE_NOT_FOUND, self::COMPOSER_EXCEPTION, self::USER_ABORTED_OPERATION, self::AUTHENTICATION_EXCEPTION, self::NOT_SUPPORTED_EXCEPTION, self::ARCHIVE_EXCEPTION, - self::SYMLINK_EXCEPTION, self::PATH_NOT_FOUND, self::GIT_EXCEPTION, self::CONFIGURATION_EXCEPTION, self::PACKAGE_EXCEPTION, - self::NETWORK_EXCEPTION + self::NETWORK_EXCEPTION, + self::INTEGRITY_EXCEPTION ]; } \ No newline at end of file diff --git a/src/ncc/Exceptions/AutoloadGeneratorException.php b/src/ncc/Exceptions/AutoloadGeneratorException.php deleted file mode 100644 index e33fabc..0000000 --- a/src/ncc/Exceptions/AutoloadGeneratorException.php +++ /dev/null @@ -1,39 +0,0 @@ -load(); } - catch (PackageLockException $e) + catch (IOException $e) { unset($e); } @@ -71,7 +70,7 @@ * Loads the PackageLock from the disk * * @return void - * @throws PackageLockException + * @throws IOException */ public function load(): void { @@ -89,7 +88,7 @@ { Console::outDebug('package lock exists, loading from disk'); $data = IO::fread($this->PackageLockPath); - if(strlen($data) > 0) + if($data !== '') { $this->PackageLock = PackageLock::fromArray(ZiProto::decode($data)); } @@ -100,7 +99,7 @@ } catch(Exception $e) { - throw new PackageLockException('The PackageLock file cannot be parsed', $e); + throw new IOException('The PackageLock file cannot be parsed', $e); } } else @@ -118,14 +117,14 @@ * * @return void * @throws AuthenticationException - * @throws PackageLockException + * @throws IOException */ public function save(): void { Console::outDebug(sprintf('saving package lock to \'%s\'', $this->PackageLockPath)); // Don't save something that isn't loaded lol - if($this->PackageLock == null) + if($this->PackageLock === null) { Console::outDebug('warning: PackageLock is null, not saving to disk'); return; @@ -144,7 +143,7 @@ } catch(IOException $e) { - throw new PackageLockException('Cannot save the package lock file to disk', $e); + throw new IOException('Cannot save the package lock file to disk', $e); } try @@ -155,7 +154,7 @@ } catch(Exception $e) { - throw new PackageLockException('Failed to synchronize symlinks', $e); + throw new IOException('Failed to synchronize symlinks', $e); } } @@ -163,7 +162,8 @@ * Constructs the package lock file if it doesn't exist * * @return void - * @throws PackageLockException + * @throws AuthenticationException + * @throws IOException */ public function constructLockFile(): void { @@ -171,7 +171,7 @@ { $this->load(); } - catch (PackageLockException $e) + catch (IOException $e) { unset($e); $this->PackageLock = new PackageLock(); @@ -182,12 +182,15 @@ /** * @return PackageLock|null - * @throws PackageLockException + * @throws IOException */ public function getPackageLock(): ?PackageLock { - if($this->PackageLock == null) + if($this->PackageLock === null) + { $this->load(); + } + return $this->PackageLock; } } \ No newline at end of file diff --git a/src/ncc/Managers/PackageManager.php b/src/ncc/Managers/PackageManager.php index a980e94..dead8c7 100644 --- a/src/ncc/Managers/PackageManager.php +++ b/src/ncc/Managers/PackageManager.php @@ -43,11 +43,8 @@ use ncc\Exceptions\IOException; use ncc\Exceptions\NotSupportedException; use ncc\Exceptions\PackageException; - use ncc\Exceptions\PackageLockException; - use ncc\Exceptions\PackageNotFoundException; use ncc\Exceptions\PathNotFoundException; use ncc\Exceptions\RunnerExecutionException; - use ncc\Exceptions\SymlinkException; use ncc\Exceptions\VersionNotFoundException; use ncc\Objects\DefinedRemoteSource; use ncc\Objects\InstallationPaths; @@ -84,7 +81,7 @@ private $package_lock_manager; /** - * @throws PackageLockException + * @throws IOException */ public function __construct() { @@ -104,13 +101,10 @@ * @throws IOException * @throws InstallationException * @throws NotSupportedException - * @throws PackageLockException - * @throws PackageNotFoundException + * @throws PackageException * @throws PathNotFoundException * @throws RunnerExecutionException - * @throws SymlinkException * @throws VersionNotFoundException - * @throws PackageException */ public function install(string $package_path, ?Entry $entry=null, array $options=[]): string { @@ -648,11 +642,8 @@ * @throws InstallationException * @throws NotSupportedException * @throws PackageException - * @throws PackageLockException - * @throws PackageNotFoundException * @throws PathNotFoundException * @throws RunnerExecutionException - * @throws SymlinkException * @throws VersionNotFoundException */ private function processDependency(Dependency $dependency, Package $package, string $package_path, ?Entry $entry=null, array $options=[]): void @@ -703,7 +694,7 @@ break; case DependencySourceType::STATIC: - throw new PackageNotFoundException('Static linking not possible, package ' . $dependency->name . ' is not installed'); + throw new PackageException('Static linking not possible, package ' . $dependency->name . ' is not installed'); case DependencySourceType::REMOTE: Console::outDebug('installing from remote source ' . $dependency->source); @@ -717,7 +708,7 @@ } elseif(!$dependency_met) { - throw new PackageNotFoundException(sprintf('Required dependency %s=%s is not installed', $dependency->name, $dependency->version)); + throw new PackageException(sprintf('Required dependency %s=%s is not installed', $dependency->name, $dependency->version)); } } @@ -726,7 +717,7 @@ * * @param string $package * @return PackageEntry|null - * @throws PackageLockException + * @throws IOException */ public function getPackage(string $package): ?PackageEntry { @@ -740,8 +731,7 @@ * @param string $package * @param string $version * @return VersionEntry|null - * @throws VersionNotFoundException - * @throws PackageLockException + * @throws IOException */ public function getPackageVersion(string $package, string $version): ?VersionEntry { @@ -754,9 +744,7 @@ * * @param string $package * @return VersionEntry|null - * @throws VersionNotFoundException - * @throws PackageLockException - * @noinspection PhpUnused + * @throws IOException */ public function getLatestVersion(string $package): ?VersionEntry { @@ -768,8 +756,7 @@ * Returns an array of all packages and their installed versions * * @return array - * @throws PackageLockException - * @throws PackageLockException + * @throws IOException */ public function getInstalledPackages(): array { @@ -797,7 +784,7 @@ $package = $this->getPackage($exploded[0]); if($package === null) { - throw new PackageNotFoundException('Package ' . $exploded[0] . ' not found'); + throw new PackageException('Package ' . $exploded[0] . ' not found'); } $version = $package->getVersion($exploded[1]); @@ -836,7 +823,7 @@ } } } - catch (PackageLockException $e) + catch (IOException $e) { unset($e); } @@ -884,10 +871,7 @@ * @return void * @throws AuthenticationException * @throws IOException - * @throws PackageLockException - * @throws PackageNotFoundException - * @throws SymlinkException - * @throws VersionNotFoundException + * @throws PackageException */ public function uninstallPackageVersion(string $package, string $version): void { @@ -899,7 +883,7 @@ $version_entry = $this->getPackageVersion($package, $version); if($version_entry === null) { - throw new PackageNotFoundException(sprintf('The package %s=%s was not found', $package, $version)); + throw new PackageException(sprintf('The package %s=%s was not found', $package, $version)); } Console::out(sprintf('Uninstalling %s=%s', $package, $version)); @@ -963,9 +947,8 @@ * @param string $package * @return void * @throws AuthenticationException - * @throws PackageLockException - * @throws PackageNotFoundException - * @throws VersionNotFoundException + * @throws IOException + * @throws PackageException */ public function uninstallPackage(string $package): void { @@ -977,7 +960,7 @@ $package_entry = $this->getPackage($package); if($package_entry === null) { - throw new PackageNotFoundException(sprintf('The package %s was not found', $package)); + throw new PackageException(sprintf('The package %s was not found', $package)); } foreach($package_entry->getVersions() as $version) diff --git a/src/ncc/Managers/ProjectManager.php b/src/ncc/Managers/ProjectManager.php index 4ec17e0..b3576da 100644 --- a/src/ncc/Managers/ProjectManager.php +++ b/src/ncc/Managers/ProjectManager.php @@ -34,7 +34,6 @@ use ncc\Exceptions\MalformedJsonException; use ncc\Exceptions\NotSupportedException; use ncc\Exceptions\PathNotFoundException; - use ncc\Exceptions\ProjectAlreadyExistsException; use ncc\Objects\ProjectConfiguration; use ncc\Objects\ProjectConfiguration\Compiler; use ncc\ThirdParty\Symfony\Uid\Uuid; @@ -105,8 +104,8 @@ * @param string|null $src * @param array $options * @throws ConfigurationException + * @throws IOException * @throws MalformedJsonException - * @throws ProjectAlreadyExistsException */ public function initializeProject(Compiler $compiler, string $name, string $package, ?string $src=null, array $options=[]): void { @@ -123,7 +122,7 @@ if(file_exists($this->project_path . DIRECTORY_SEPARATOR . 'project.json')) { - throw new ProjectAlreadyExistsException('A project has already been initialized in \'' . $this->project_path . DIRECTORY_SEPARATOR . 'project.json' . '\''); + throw new IOException('A project has already been initialized in \'' . $this->project_path . DIRECTORY_SEPARATOR . 'project.json' . '\''); } $this->project_configuration = new ProjectConfiguration(); diff --git a/src/ncc/Managers/SymlinkManager.php b/src/ncc/Managers/SymlinkManager.php index 0cc704e..931e2e7 100644 --- a/src/ncc/Managers/SymlinkManager.php +++ b/src/ncc/Managers/SymlinkManager.php @@ -27,7 +27,7 @@ use Exception; use ncc\Enums\Scopes; use ncc\Exceptions\AuthenticationException; - use ncc\Exceptions\SymlinkException; + use ncc\Exceptions\IOException; use ncc\Objects\SymlinkDictionary\SymlinkEntry; use ncc\ThirdParty\Symfony\Filesystem\Filesystem; use ncc\Utilities\Console; @@ -85,7 +85,6 @@ * * @return void * @throws AuthenticationException - * @throws SymlinkException */ public function load(): void { @@ -130,7 +129,7 @@ * @param bool $throw_exception * @return void * @throws AuthenticationException - * @throws SymlinkException + * @throws IOException */ private function save(bool $throw_exception=true): void { @@ -154,7 +153,9 @@ catch(Exception $e) { if($throw_exception) - throw new SymlinkException(sprintf('failed to save symlink dictionary to %s', $this->SymlinkDictionaryPath), $e); + { + throw new IOException(sprintf('failed to save symlink dictionary to %s', $this->SymlinkDictionaryPath), $e); + } Console::outWarning(sprintf('failed to save symlink dictionary to %s', $this->SymlinkDictionaryPath)); } @@ -204,7 +205,7 @@ * @param string $unit * @return void * @throws AuthenticationException - * @throws SymlinkException + * @throws IOException */ public function add(string $package, string $unit='main'): void { @@ -232,7 +233,7 @@ * @param string $package * @return void * @throws AuthenticationException - * @throws SymlinkException + * @throws IOException */ public function remove(string $package): void { @@ -258,7 +259,9 @@ $symlink = self::$BinPath . DIRECTORY_SEPARATOR . $symlink_name; if($filesystem->exists($symlink)) + { $filesystem->remove($symlink); + } } unset($this->SymlinkDictionary[$key]); @@ -267,7 +270,7 @@ } } - throw new SymlinkException(sprintf('failed to remove package %s from the symlink dictionary', $package)); + throw new IOException(sprintf('failed to remove package %s from the symlink dictionary', $package)); } /** @@ -276,7 +279,6 @@ * @param string $package * @return void * @throws AuthenticationException - * @throws SymlinkException */ private function setAsRegistered(string $package): void { @@ -297,7 +299,6 @@ * * @return void * @throws AuthenticationException - * @throws SymlinkException */ public function sync(): void { @@ -328,7 +329,7 @@ { $package_entry = $package_lock_manager->getPackageLock()->getPackage($entry->Package); - if($package_entry == null) + if($package_entry === null) { Console::outWarning(sprintf('Package %s is not installed, skipping', $entry->Package)); continue; diff --git a/src/ncc/Objects/Constant.php b/src/ncc/Objects/Constant.php index a4ec300..0522fff 100644 --- a/src/ncc/Objects/Constant.php +++ b/src/ncc/Objects/Constant.php @@ -1,28 +1,31 @@ Scope = $scope; - $this->Name = $name; - $this->Value = $value; - $this->Readonly = $readonly; - $this->Hash = Resolver::resolveConstantHash($this->Scope, $this->Name); + $this->scope = $scope; + $this->name = $name; + $this->value = $value; + $this->readonly = $readonly; + $this->hash = Resolver::resolveConstantHash($this->scope, $this->name); } /** @@ -86,7 +89,7 @@ namespace ncc\Objects; */ public function __toString(): string { - return $this->Value; + return $this->value; } /** @@ -94,7 +97,7 @@ namespace ncc\Objects; */ public function getValue(): string { - return $this->Value; + return $this->value; } /** @@ -104,23 +107,23 @@ namespace ncc\Objects; */ public function getFullName(): string { - return Resolver::resolveFullConstantName($this->Scope, $this->Name); + return Resolver::resolveFullConstantName($this->scope, $this->name); } /** * @param string $value * @param bool $readonly - * @throws ConstantReadonlyException + * @throws IntegrityException */ public function setValue(string $value, bool $readonly=false): void { - if($this->Readonly) + if($this->readonly) { - throw new ConstantReadonlyException('Cannot set value to the constant \'' . $this->getFullName() . '\', constant is readonly'); + throw new IntegrityException('Cannot set value to the constant \'' . $this->getFullName() . '\', constant is readonly'); } - $this->Value = $value; - $this->Readonly = $readonly; + $this->value = $value; + $this->readonly = $readonly; } /** @@ -128,7 +131,7 @@ namespace ncc\Objects; */ public function isReadonly(): bool { - return $this->Readonly; + return $this->readonly; } /** @@ -136,7 +139,7 @@ namespace ncc\Objects; */ public function getHash(): string { - return $this->Hash; + return $this->hash; } /** @@ -144,7 +147,7 @@ namespace ncc\Objects; */ public function getScope(): string { - return $this->Scope; + return $this->scope; } /** @@ -152,6 +155,6 @@ namespace ncc\Objects; */ public function getName(): string { - return $this->Name; + return $this->name; } } \ No newline at end of file diff --git a/src/ncc/Objects/PackageLock/PackageEntry.php b/src/ncc/Objects/PackageLock/PackageEntry.php index 3391708..7645cc2 100644 --- a/src/ncc/Objects/PackageLock/PackageEntry.php +++ b/src/ncc/Objects/PackageLock/PackageEntry.php @@ -1,24 +1,24 @@ Name)); } return null; @@ -163,7 +163,7 @@ $this->removeVersion($package->assembly->version); } } - catch (VersionNotFoundException $e) + catch (IOException $e) { unset($e); } diff --git a/src/ncc/Runtime.php b/src/ncc/Runtime.php index f18051b..c264b4f 100644 --- a/src/ncc/Runtime.php +++ b/src/ncc/Runtime.php @@ -28,12 +28,11 @@ use ncc\Enums\CompilerExtensions; use ncc\Enums\Versions; use ncc\Classes\PhpExtension\PhpRuntime; - use ncc\Exceptions\ConstantReadonlyException; + use ncc\Exceptions\ConfigurationException; use ncc\Exceptions\ImportException; - use ncc\Exceptions\InvalidConstantNameException; - use ncc\Exceptions\PackageLockException; - use ncc\Exceptions\PackageNotFoundException; - use ncc\Exceptions\VersionNotFoundException; + use ncc\Exceptions\IntegrityException; + use ncc\Exceptions\IOException; + use ncc\Exceptions\PackageException; use ncc\Managers\PackageManager; use ncc\Objects\PackageLock\VersionEntry; use ncc\Objects\ProjectConfiguration\Dependency; @@ -57,7 +56,7 @@ * @param string $package * @param string $version * @return bool - * @throws PackageLockException + * @throws IOException */ private static function isImported(string $package, string $version=Versions::LATEST): bool { @@ -90,6 +89,7 @@ * @param string $version * @param array $options * @return void + * @throws IOException * @throws ImportException */ public static function import(string $package, string $version=Versions::LATEST, array $options=[]): void @@ -98,10 +98,11 @@ { $package_entry = self::getPackageManager()->getPackage($package); } - catch (PackageLockException $e) + catch (IOException $e) { throw new ImportException(sprintf('Failed to import package "%s" due to a package lock exception: %s', $package, $e->getMessage()), $e); } + if($package_entry === null) { throw new ImportException(sprintf("Package '%s' not found", $package)); @@ -122,7 +123,7 @@ throw new ImportException(sprintf('Version %s of %s is not installed', $version, $package)); } } - catch (VersionNotFoundException $e) + catch (IOException $e) { throw new ImportException(sprintf('Version %s of %s is not installed', $version, $package), $e); } @@ -134,7 +135,7 @@ return; } } - catch (PackageLockException $e) + catch (IOException $e) { throw new ImportException(sprintf('Failed to check if package %s is imported', $package), $e); } @@ -174,8 +175,9 @@ * * @param string $package * @return string - * @throws PackageLockException - * @throws PackageNotFoundException + * @throws ConfigurationException + * @throws IOException + * @throws PackageException */ public static function getDataPath(string $package): string { @@ -183,7 +185,7 @@ if($package === null) { - throw new PackageNotFoundException('Package not found (null entry error, possible bug)'); + throw new PackageException('Package not found (null entry error, possible bug)'); } return $package->getDataPath(); @@ -231,8 +233,7 @@ * @param string $name * @param string $value * @return void - * @throws ConstantReadonlyException - * @throws InvalidConstantNameException + * @throws IntegrityException */ public static function setConstant(string $package, string $name, string $value): void { diff --git a/src/ncc/Runtime/Constants.php b/src/ncc/Runtime/Constants.php index 2b8195a..bf33b66 100644 --- a/src/ncc/Runtime/Constants.php +++ b/src/ncc/Runtime/Constants.php @@ -24,8 +24,8 @@ namespace ncc\Runtime; - use ncc\Exceptions\ConstantReadonlyException; - use ncc\Exceptions\InvalidConstantNameException; + use InvalidArgumentException; + use ncc\Exceptions\IntegrityException; use ncc\Objects\Constant; use ncc\Utilities\Resolver; use ncc\Utilities\Validate; @@ -47,13 +47,14 @@ * @param string $value The value of the constant * @param bool $readonly Indicates if the constant cannot be changed with the registerConstant function once it's registered * @return void - * @throws ConstantReadonlyException - * @throws InvalidConstantNameException + * @throws IntegrityException */ public static function register(string $scope, string $name, string $value, bool $readonly=false): void { if(!Validate::constantName($name)) - throw new InvalidConstantNameException('The name specified is not valid for a constant name'); + { + throw new InvalidArgumentException(sprintf('The name \'%s\' is not a valid constant name', $name)); + } $constant_hash = Resolver::resolveConstantHash($scope, $name); @@ -72,18 +73,20 @@ * @param string $scope * @param string $name * @return void - * @throws ConstantReadonlyException + * @throws IntegrityException */ public static function delete(string $scope, string $name): void { if(!Validate::constantName($name)) + { return; + } $constant_hash = Resolver::resolveConstantHash($scope, $name); if(isset(self::$Constants[$constant_hash]) && self::$Constants[$constant_hash]->isReadonly()) { - throw new ConstantReadonlyException('Cannot delete the constant \'' . self::$Constants[$constant_hash]->getFullName() . '\', constant is readonly'); + throw new IntegrityException('Cannot delete the constant \'' . self::$Constants[$constant_hash]->getFullName() . '\', constant is readonly'); } unset(self::$Constants[$constant_hash]); @@ -99,12 +102,16 @@ public static function get(string $scope, string $name): ?string { if(!Validate::constantName($name)) + { return null; + } $constant_hash = Resolver::resolveConstantHash($scope, $name); if(isset(self::$Constants[$constant_hash])) + { return self::$Constants[$constant_hash]->getValue(); + } return null; }