diff --git a/CHANGELOG.md b/CHANGELOG.md index c4f2ca5..34a38bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -199,6 +199,18 @@ features and reduced the number of exceptions down to 15 exceptions. - Updated class `\ncc\Objects > RemotePackageInput` to use method calls rather than direct property access - Updated class `\ncc\Objects > RepositoryQueryResults` to use method calls rather than direct property access - Updated class `\ncc\Objects > Vault` to use method calls rather than direct property access + - `\ncc\Objects\ProjectConfiguration > Compiler`: Added Public Constructor to automatically determine the minimum and + maximum supported compiler version for the selected extension + - `\ncc\Objects\ProjectConfiguration > Compiler > fromArray()` throws an ConfigurationException if the property `extension` is null + - `\ncc\Objects\ProjectConfiguration > Compiler > fromArray()` throws an NotSupportedException if the `extension` uses an + unsupported compiler extension + - `\ncc\Objects\ProjectConfiguration > Compiler > validate()` No longer accepts `$throw_exception` and throws an + `ConfigurationException` or `NotSupportedException` if the validation fails, otherwise it returns `True`. + - `\ncc\Objects\ProjectConfiguration > Project > fromArray()` Throws an `ConfigurationException` if the property `compiler` + is missing in the project configuration + - `\ncc\Objects > ProjectConfiguration > fromArray()` Throws an `ConfigurationException` if the property 'project' is + missing in the root configuration + - `\ncc\Objects\ProjectConfiguration > Project > __construct()` now requires the parameter `$compiler` ### Removed @@ -261,6 +273,7 @@ features and reduced the number of exceptions down to 15 exceptions. - Removed unused `scope` property from `\ncc\Objects\ProjectConfiguration > Build` - Removed unused `\ncc\Objects > NccUpdateInformation` - Removed unused `\ncc\Objects > PhpConfiguration` + - Removed parameter `$throw_exception` from `\ncc\Objects\ProjectConfiguration > Project > validate()` diff --git a/src/ncc/Managers/ProjectManager.php b/src/ncc/Managers/ProjectManager.php index ae3eccc..2b425ec 100644 --- a/src/ncc/Managers/ProjectManager.php +++ b/src/ncc/Managers/ProjectManager.php @@ -208,15 +208,14 @@ * Attempts to load the project configuration * * @return void - * @throws ConfigurationException * @throws IOException * @throws PathNotFoundException */ public function load(): void { - if(!file_exists($this->project_file_path) && !is_file($this->project_file_path)) + if(!is_file($this->project_file_path)) { - throw new ConfigurationException('The project configuration file \'' . $this->project_file_path . '\' was not found'); + throw new PathNotFoundException($this->project_file_path); } $this->project_configuration = ProjectConfiguration::fromFile($this->project_file_path); diff --git a/src/ncc/Objects/ProjectConfiguration.php b/src/ncc/Objects/ProjectConfiguration.php index 65f950a..564311b 100644 --- a/src/ncc/Objects/ProjectConfiguration.php +++ b/src/ncc/Objects/ProjectConfiguration.php @@ -551,6 +551,10 @@ /** * @inheritDoc + * @param array $data + * @return ProjectConfiguration + * @throws ConfigurationException + * @throws NotSupportedException */ public static function fromArray(array $data): ProjectConfiguration { @@ -561,6 +565,10 @@ { $object->project = Project::fromArray($object->project); } + else + { + throw new ConfigurationException('The project configuration is missing the required property "project" in the root of the configuration'); + } $object->assembly = Functions::array_bc($data, 'assembly'); if($object->assembly !== null) diff --git a/src/ncc/Objects/ProjectConfiguration/Compiler.php b/src/ncc/Objects/ProjectConfiguration/Compiler.php index d5ffc38..b2017d5 100644 --- a/src/ncc/Objects/ProjectConfiguration/Compiler.php +++ b/src/ncc/Objects/ProjectConfiguration/Compiler.php @@ -1,4 +1,5 @@ extension = $extension; + $this->minimum_version = $minimum_version; + $this->maximum_version = $maximum_version; + } + /** * @return string */ @@ -111,53 +151,32 @@ /** * Validates the compiler object * - * @param bool $throw_exception * @return bool * @throws ConfigurationException * @throws NotSupportedException */ - public function validate(bool $throw_exception=True): bool + public function validate(): bool { if($this->extension === null) { - if($throw_exception) - { - throw new ConfigurationException('The property \'extension\' must not be null.'); - } - - return False; + throw new ConfigurationException('The property \'extension\' must not be null.'); } if($this->minimum_version === null) { - if($throw_exception) - { - throw new ConfigurationException('The property \'minimum_version\' must not be null.'); - } - - return False; + throw new ConfigurationException('The property \'minimum_version\' must not be null.'); } if($this->maximum_version === null) { - if($throw_exception) - { - throw new ConfigurationException('The property \'maximum_version\' must not be null.'); - } - - return False; + throw new ConfigurationException('The property \'maximum_version\' must not be null.'); } try { if(VersionComparator::compareVersion($this->minimum_version, $this->maximum_version) === 1) { - if($throw_exception) - { - throw new ConfigurationException('The minimum version cannot be greater version number than the maximum version'); - } - - return False; + throw new ConfigurationException('The minimum version cannot be greater version number than the maximum version'); } } catch (Exception $e) @@ -165,40 +184,32 @@ throw new ConfigurationException('Version comparison failed: ' . $e->getMessage()); } - if(!in_array($this->extension, CompilerExtensions::ALL)) - { - if($throw_exception) - { - throw new NotSupportedException('The compiler extension \'' . $this->extension . '\' is not supported'); - } - - return False; - } + /** @noinspection InArrayMissUseInspection */ + if(!in_array($this->extension, CompilerExtensions::ALL, true)) + { + throw new NotSupportedException('The compiler extension \'' . $this->extension . '\' is not supported'); + } + /** @noinspection DegradedSwitchInspection */ switch($this->extension) { case CompilerExtensions::PHP: - if(!in_array($this->maximum_version, CompilerExtensionSupportedVersions::PHP)) + + if(!in_array($this->maximum_version, CompilerExtensionSupportedVersions::PHP, true)) { - if($throw_exception) - { - throw new NotSupportedException('The MaximumVersion does not support version ' . $this->maximum_version . ' for the extension ' . $this->extension); - } - return False; + throw new NotSupportedException('The property "project.compiler.maximum_version" does not support version ' . $this->maximum_version . ' for the extension ' . $this->extension); + } - if(!in_array($this->minimum_version, CompilerExtensionSupportedVersions::PHP)) + if(!in_array($this->minimum_version, CompilerExtensionSupportedVersions::PHP, true)) { - if($throw_exception) - { - throw new NotSupportedException('The MinimumVersion does not support version ' . $this->minimum_version . ' for the extension ' . $this->extension); - } - return False; + throw new NotSupportedException('The property "project.compiler.minimum_version" does not support version ' . $this->minimum_version . ' for the extension ' . $this->extension); } + break; default: - throw new NotSupportedException('The compiler extension \'' . $this->extension . '\' is not supported'); + throw new NotSupportedException('The compiler extension "' . $this->extension . '" is not supported by ncc'); } return True; @@ -230,15 +241,21 @@ /** * @inheritDoc + * @param array $data + * @return Compiler + * @throws ConfigurationException + * @throws NotSupportedException */ public static function fromArray(array $data): Compiler { - $object = new self(); + if(Functions::array_bc($data, 'extension') === null) + { + throw new ConfigurationException('The property \'project.compiler.extension\' must not be null.'); + } - $object->maximum_version = Functions::array_bc($data, 'maximum_version'); - $object->extension = Functions::array_bc($data, 'extension'); - $object->minimum_version = Functions::array_bc($data, 'minimum_version'); - - return $object; + return new self(Functions::array_bc($data, 'extension'), + Functions::array_bc($data, 'maximum_version'), + Functions::array_bc($data, 'minimum_version') + ); } } \ No newline at end of file diff --git a/src/ncc/Objects/ProjectConfiguration/Project.php b/src/ncc/Objects/ProjectConfiguration/Project.php index e310d8c..99bf0a2 100644 --- a/src/ncc/Objects/ProjectConfiguration/Project.php +++ b/src/ncc/Objects/ProjectConfiguration/Project.php @@ -53,9 +53,9 @@ /** * Public Constructor */ - public function __construct() + public function __construct(Compiler $compiler) { - $this->compiler = new Compiler(); + $this->compiler = $compiler; $this->options = []; } @@ -129,17 +129,13 @@ /** * Validates the Project object * - * @param bool $throw_exception * @return bool * @throws ConfigurationException * @throws NotSupportedException */ - public function validate(bool $throw_exception=True): bool + public function validate(): bool { - if(!$this->compiler->validate($throw_exception)) - { - return False; - } + $this->compiler->validate(); return True; } @@ -170,22 +166,28 @@ * * @param array $data * @return Project + * @throws ConfigurationException + * @throws NotSupportedException */ public static function fromArray(array $data): Project { - $object = new self(); - if(Functions::array_bc($data, 'compiler') !== null) { - $object->compiler = Compiler::fromArray(Functions::array_bc($data, 'compiler')); + $object = new self(Compiler::fromArray(Functions::array_bc($data, 'compiler'))); } - - if(Functions::array_bc($data, 'options') !== null) + else { - $object->options = Functions::array_bc($data, 'options'); + throw new ConfigurationException('The project configuration is missing the required property "compiler" in the project section.'); } - if(Functions::array_bc($data, 'update_source') !== null) + $object->options = Functions::array_bc($data, 'options'); + if($object->options === null) + { + $object->options = []; + } + + $object->update_source = Functions::array_bc($data, 'update_source'); + if($object->update_source !== null) { $object->update_source = UpdateSource::fromArray(Functions::array_bc($data, 'update_source')); } diff --git a/tests/projects/php_cli/project.json b/tests/projects/php_cli/project.json new file mode 100644 index 0000000..571b4c8 --- /dev/null +++ b/tests/projects/php_cli/project.json @@ -0,0 +1,8 @@ +{ + "project": { + "compiler": { + "extension": "php", + "minimum_version": "8.0" + } + } +} \ No newline at end of file