Skip to content

Commit

Permalink
native php eunis (#204)
Browse files Browse the repository at this point in the history
fixes #140

* raise php requirement from `8.0` to `8.1`
* convert all enum classes to be native PHP enums


Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck committed Feb 4, 2023
1 parent d41d700 commit 9a83010
Show file tree
Hide file tree
Showing 60 changed files with 462 additions and 601 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/php-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ jobs:
- "windows-latest"
php:
- "8.2" # highest supported
- "8.1"
- "8.0" # lowest supported
- "8.1" # lowest supported
timeout-minutes: 10
steps:
- name: Checkout
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ jobs:
os: [ "ubuntu-latest" ]
php:
- "8.2" # highest supported
- "8.1"
- "8.0" # lowest supported
- "8.1" # lowest supported
dependencies: [ "lowest", "highest" ]
exclude:
- # unstable combinations: lowest `opis/json-schemaa` on php8.2 - throws PHP Deprecated warnings
Expand Down Expand Up @@ -122,7 +121,7 @@ jobs:
php: "8.2"
dependencies: "highest"
- # lowest supported
php: "8.0"
php: "8.1"
dependencies: "lowest"
steps:
- name: Checkout
Expand Down
8 changes: 5 additions & 3 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
return (new PhpCsFixer\Config())
->setUsingCache(true)
->setRules(
// docs: https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/rules/index.rst
// assistance via tool: https://mlocati.github.io/php-cs-fixer-configurator/
/* docs: https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/rules/index.rst
* docs: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/master/doc/ruleSets/index.rst
* assistance via tool: https://mlocati.github.io/php-cs-fixer-configurator/
*/
[
'@PHP80Migration' => true,
'@PHP81Migration' => true,
'@PHP80Migration:risky' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
Expand Down
23 changes: 20 additions & 3 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ All notable changes to this project will be documented in this file.
* BREAKING
* Removed support for PHP v7.3. ([#6] via [#125])
* Removed support for PHP v7.4. ([#114] via [#125])
* Removed support for PHP v8.0. (via [#204])
* Changed models' aggregation properties to be no longer optional. ([#66] via [#131])
* Streamlined repository data structures to follow a common method naming scheme. (via [#131])
* Enum classes were converted to native PHP Enums ([#140] via [#204])
* Added
* Support for CycloneDX v1.4 ([#57] via [#65], [#118], [#123])
* Support for [properties](https://cyclonedx.org/use-cases/#properties--name-value-store) in data models (via [#165])
Expand All @@ -36,8 +38,15 @@ All notable changes to this project will be documented in this file.
* `\CycloneDX\Core\Enum` namespace
* `Classification` class
* BREAKING: renamed class to `ComponentType` (via [#170])
* BREAKING: became a native PHP enum ([#140] via [#204])
* BREAKING: method `isValidValue()` was removed (via [#204])
* `ExternalReferenceType` class
* Added class constant `::RELEASE_NOTES` to reflect CycloneDX v1.4 ([#57] via [#65])
* BREAKING: became a native PHP enum ([#140] via [#204])
* BREAKING: method `isValidValue()` was removed (via [#204])
* Added enum case `RELEASE_NOTES` to reflect CycloneDX v1.4 ([#57] via [#65])
* `HashAlgorithm` class
* BREAKING: became a native PHP enum ([#140] via [#204])
* BREAKING: method `isValidValue()` was removed (via [#204])
* `CycloneDX\Core\Factories` namespace
* `LicenseFactory` class
* Breaking: removed method `makeDisjunctiveFromExpression()` ([#163] vial [#166])
Expand All @@ -61,13 +70,17 @@ All notable changes to this project will be documented in this file.
and changed parameter & return type to non-nullable, was nullable ([#66] via [#131])
* BREAKING: renamed methods `{get,set}License()` -> `{get,set}Licenses()` (via [#131])
and changed it work with class `LicenseRepository` only, was working with various `Models\License\*` types. ([#66] via [#131])
* BREAKING: Changed class property `version` to optional now, to reflect CycloneDX v1.4. ([#27] via [#118], [#131])
* BREAKING: changed class property `version` to be optional, to reflect CycloneDX v1.4. ([#27] via [#118], [#131])
This affects constructor arguments, and affects methods `{get,set}Version()`.
* BREAKING: changed property `type` to be of type `\CycloneDX\Core\Enum\ComponentType` ([#140] via [#204])
This affects constructor arguments, and affects methods `{get,set}Type()`.
* Added `{get,set}Author()` ([#184] via [#185])
* Added `{get,set}Properties()` (via [#165])
* `ExternalReference` class
* BREAKING: renamed methods `{get,set}HashRepository()` -> `{get,set}Hashes()` ([#133] via [#131])
and changed parameter & return type to non-nullable, was nullable ([#66] via [#131])
* BREAKING: changed property `type` to be of type `\CycloneDX\Core\Enum\ExternalReferenceType` ([#140] via [#204])
This affects constructor arguments, and affects methods `{get,set}Type()`.
* `Licenses` namespace
* `AbstractDisjunctiveLicense`
* BREAKING: removed this class (via [#125], [#131])
Expand Down Expand Up @@ -100,6 +113,7 @@ All notable changes to this project will be documented in this file.
* `HashRepository` class
* BREAKING: renamed to `\CycloneDX\Core\Collections\HashDictionary` ([#133] via [#131])
* BREAKING: renamed all methods and changed all method signatures to match the overall streamlined scheme ([#133] via [#131])
* BREAKING: changed all method signatures to enable handling of enum type `\CycloneDX\Core\Enum\HashAlgorithm` ([#140] via [#204])
* `\CycloneDX\Core\Serialize` namespace
* Overall
* BREAKING: renamed namespace to `Serialization` ([#5] via [#146])
Expand All @@ -125,6 +139,7 @@ All notable changes to this project will be documented in this file.
* BREAKING: removed classes `DisjunctiveLicenseRepositoryNormalizer` (via [#131])
* BREAKING: renamed classes `HashRepositoryNormalizer` -> `HashDictionaryNormalizer` ([#133] via [#131])
and changed signatures to accept `Models\HashDictionary` instead of `Models\HashRepository`
* BREAKING: changed signatures of class `HashNormalizer` to accept enum `\CycloneDX\Core\Enum\HashAlgorithm` ([#140] via [#204])
* Added new classes `LicenseNormalizer` that can normalize every existing license model (via [#131])
* Added new classes `LicenseRepositoryNormalizer` that can normalize `LicenseRepository` (via [#131])
* `ExternalReferenceNormalizer` classes
Expand All @@ -139,7 +154,7 @@ All notable changes to this project will be documented in this file.
* `\CycloneDX\Core\Spdx` namespace
* BREAKING: renamed the class `License` -> `LicenseValidator` ([#133] via [#143])
* `\CycloneDX\Core\Spec` namespace
* BREAKING: completely reworked everything ([#139] via [#142], [#174])
* BREAKING: completely reworked everything ([#139] via [#142], [#174], [#204])
See the code base for references
* `\CycloneDX\Core\Validation` namespace
* `BaseValidator` class
Expand Down Expand Up @@ -170,6 +185,7 @@ All notable changes to this project will be documented in this file.
[#133]: https://github.com/CycloneDX/cyclonedx-php-library/pull/133
[#137]: https://github.com/CycloneDX/cyclonedx-php-library/issues/137
[#139]: https://github.com/CycloneDX/cyclonedx-php-library/issues/139
[#140]: https://github.com/CycloneDX/cyclonedx-php-library/issues/140
[#142]: https://github.com/CycloneDX/cyclonedx-php-library/pull/142
[#143]: https://github.com/CycloneDX/cyclonedx-php-library/pull/143
[#144]: https://github.com/CycloneDX/cyclonedx-php-library/pull/144
Expand All @@ -190,6 +206,7 @@ All notable changes to this project will be documented in this file.
[#186]: https://github.com/CycloneDX/cyclonedx-php-library/pull/186
[#187]: https://github.com/CycloneDX/cyclonedx-php-library/issues/187
[#188]: https://github.com/CycloneDX/cyclonedx-php-library/pull/188
[#204]: https://github.com/CycloneDX/cyclonedx-php-library/pull/204

## 1.6.3 - 2022-09-15

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"source": "https://github.com/CycloneDX/cyclonedx-php-library/"
},
"require": {
"php": "^8.0",
"php": "^8.1",
"ext-dom": "*",
"ext-json": "*",
"ext-libxml": "*",
Expand Down
66 changes: 28 additions & 38 deletions src/Core/Collections/HashDictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,101 +25,91 @@

use Countable;
use CycloneDX\Core\Enums\HashAlgorithm;
use DomainException;

/**
* Dictionary of {@see \CycloneDX\Core\Enums\HashAlgorithm} => HashContent.
*
* @psalm-type HashContent = string
* @psalm-type HashAlgorithmContentTuple = array{0:HashAlgorithm,1:HashContent}
*
* @author jkowalleck
*/
class HashDictionary implements Countable
{
/**
* @var string[] dictionary of hashes
*
* @psalm-var array<HashAlgorithm::*,HashContent>
* @psalm-var array<string,HashAlgorithmContentTuple>
*/
private array $items = [];

/**
* Ignores unknown hash algorithms.
*
* @param string[] $items dictionary of hashes. Valid keys are {@see \CycloneDX\Core\Enums\HashAlgorithm}
* @param array $items list of tuples of [{@see \CycloneDX\Core\Enums\HashAlgorithm} `$algorithm`, string `$content`]
*
* @psalm-param array<string|HashAlgorithm::*,string|HashContent> $items
* @psalm-param array<HashAlgorithmContentTuple> $items
*/
public function __construct(array $items = [])
public function __construct(array ...$items)
{
$this->setItems($items);
$this->setItems(...$items);
}

/**
* Set the hashes.
* Ignores unknown hash algorithms.
*
* @param string[] $items dictionary of hashes. Valid keys are {@see \CycloneDX\Core\Enums\HashAlgorithm}
* @param array $items list of tuples of [{@see \CycloneDX\Core\Enums\HashAlgorithm} `$algorithm`, string `$content`]
*
* @psalm-param array<string|HashAlgorithm::*,string|HashContent> $items
* @psalm-param array<HashAlgorithmContentTuple> $items
*
* @return $this
*/
public function setItems(array $items): self
public function setItems(array ...$items): self
{
foreach ($items as $algorithm => $content) {
try {
$this->set($algorithm, $content);
} catch (DomainException) {
// pass
}
foreach ($items as [$algorithm, $content]) {
$this->set($algorithm, $content);
}

return $this;
}

/**
* @return string[] dictionary of hashes
* @return array[] list of tuples of [{@see \CycloneDX\Core\Enums\HashAlgorithm} `$algorithm`, string `$content`]
*
* @psalm-return array<HashAlgorithm::*,HashContent>
* @psalm-return list<HashAlgorithmContentTuple>
*/
public function getItems(): array
{
return $this->items;
return array_values($this->items);
}

/**
* @psalm-assert HashAlgorithm::* $algorithm
*
* @throws DomainException if $algorithm is not in {@see \CycloneDX\Core\Enums\HashAlgorithm}'s constants list
*
* @return $this
*
* @SuppressWarnings(PHPMD.StaticAccess)
* @SuppressWarnings(PHPMD.ElseExpression)
*/
public function set(string $algorithm, ?string $content): self
public function set(HashAlgorithm $algorithm, ?string $content): self
{
if (false === HashAlgorithm::isValidValue($algorithm)) {
throw new DomainException("Unknown hash algorithm: $algorithm");
}

if (null === $content) {
unset($this->items[$algorithm]);
$key = self::makeDictKey($algorithm);
if (null === $content || '' === $content) {
unset($this->items[$key]);
} else {
// no validation. content is schema-specific and may vary from CycloneDX spec to another.
$this->items[$algorithm] = $content;
$this->items[$key] = [$algorithm, $content];
}

return $this;
}

/**
* @psalm-param HashAlgorithm::*|string $algorithm
*/
public function get(string $algorithm): ?string
public function get(HashAlgorithm $algorithm): ?string
{
return $this->items[self::makeDictKey($algorithm)][1]
?? null;
}

/** @psalm-pure */
private static function makeDictKey(HashAlgorithm $algorithm): string
{
return $this->items[$algorithm] ?? null;
return $algorithm->value;
}

/**
Expand Down
30 changes: 9 additions & 21 deletions src/Core/Enums/ComponentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

namespace CycloneDX\Core\Enums;

use ReflectionClass;

/**
* Classification - aka ComponentType.
*
Expand All @@ -36,24 +34,14 @@
*
* @author jkowalleck
*/
abstract class ComponentType
enum ComponentType: string
{
public const APPLICATION = 'application';
public const FRAMEWORK = 'framework';
public const LIBRARY = 'library';
public const OPERATING_SYSTEMS = 'operating-system';
public const DEVICE = 'device';
public const FILE = 'file';
public const CONTAINER = 'container';
public const FIRMWARE = 'firmware';

/**
* @psalm-assert-if-true self::* $value
*/
public static function isValidValue(string $value): bool
{
$values = (new ReflectionClass(self::class))->getConstants();

return \in_array($value, $values, true);
}
case APPLICATION = 'application';
case FRAMEWORK = 'framework';
case LIBRARY = 'library';
case OPERATING_SYSTEMS = 'operating-system';
case DEVICE = 'device';
case FILE = 'file';
case CONTAINER = 'container';
case FIRMWARE = 'firmware';
}
Loading

0 comments on commit 9a83010

Please sign in to comment.