Skip to content

BC: readonly public access no getter #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions src/Core/Factories/LicenseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,11 @@
class LicenseFactory
{
public function __construct(
private readonly LicenseIdentifiers $licenseIdentifiers = new LicenseIdentifiers(),
private readonly SpdxLicenses $spdxLicenses = new SpdxLicenses()
public readonly LicenseIdentifiers $licenseIdentifiers = new LicenseIdentifiers(),
public readonly SpdxLicenses $spdxLicenses = new SpdxLicenses()
) {
}

public function getLicenseIdentifiers(): LicenseIdentifiers
{
return $this->licenseIdentifiers;
}

public function getSpdxLicenses(): SpdxLicenses
{
return $this->spdxLicenses;
}

public function makeFromString(string $license): SpdxLicense|LicenseExpression|NamedLicense
{
try {
Expand Down
21 changes: 5 additions & 16 deletions src/Core/Serialization/DOM/NormalizerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,22 @@ class NormalizerFactory
{
public const FORMAT = Format::XML;

private readonly Spec $spec;

private readonly DOMDocument $document;
public readonly Spec $spec;

/**
* @throws DomainException when the spec does not support XML format
*/
public function __construct(Spec $spec)
{
public function __construct(
Spec $spec,
public readonly DOMDocument $document = new DOMDocument()
) {
$this->spec = $spec->isSupportedFormat(self::FORMAT)
? $spec
: throw new DomainException('Unsupported format "'.self::FORMAT->name.'" for spec '.$spec->getVersion()->name);
$this->document = new DOMDocument();
}

// intention: all factory methods return an instance of "_BaseNormalizer"

public function getSpec(): Spec
{
return $this->spec;
}

public function getDocument(): DOMDocument
{
return $this->document;
}

public function makeForBom(): Normalizers\BomNormalizer
{
return new Normalizers\BomNormalizer($this);
Expand Down
33 changes: 16 additions & 17 deletions src/Core/Serialization/DOM/Normalizers/BomNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ class BomNormalizer extends _BaseNormalizer

public function normalize(Bom $bom): DOMElement
{
$factory = $this->getNormalizerFactory();
$document = $factory->getDocument();
$factory = $this->normalizerFactory;

$element = $document->createElementNS(
sprintf(self::XML_NAMESPACE_PATTERN, $factory->getSpec()->getVersion()->value),
$element = $factory->document->createElementNS(
sprintf(self::XML_NAMESPACE_PATTERN, $factory->spec->getVersion()->value),
'bom' // no namespace = defaultNS - so children w/o NS fall under this NS
);
SimpleDOM::setAttributes(
Expand Down Expand Up @@ -74,19 +73,19 @@ public function normalize(Bom $bom): DOMElement

private function normalizeComponents(ComponentRepository $components): DOMElement
{
$factory = $this->getNormalizerFactory();
$factory = $this->normalizerFactory;

return SimpleDOM::appendChildren(
$factory->getDocument()->createElement('components'),
$factory->document->createElement('components'),
$factory->makeForComponentRepository()->normalize($components)
);
}

private function normalizeMetadata(Metadata $metadata): ?DOMElement
{
$factory = $this->getNormalizerFactory();
$factory = $this->normalizerFactory;

if (false === $factory->getSpec()->supportsMetadata()) {
if (false === $factory->spec->supportsMetadata()) {
return null;
}

Expand All @@ -99,11 +98,11 @@ private function normalizeMetadata(Metadata $metadata): ?DOMElement

private function normalizeExternalReferences(Bom $bom): ?DOMElement
{
$factory = $this->getNormalizerFactory();
$factory = $this->normalizerFactory;

$extRefs = $bom->getExternalReferences();

if (false === $factory->getSpec()->supportsMetadata()) {
if (false === $factory->spec->supportsMetadata()) {
// prevent possible information loss: metadata cannot be rendered -> put it to bom
$mcr = $bom->getMetadata()->getComponent()?->getExternalReferences();
if (null !== $mcr) {
Expand All @@ -121,16 +120,16 @@ private function normalizeExternalReferences(Bom $bom): ?DOMElement
return 0 === \count($refs)
? null
: SimpleDOM::appendChildren(
$factory->getDocument()->createElement('externalReferences'),
$factory->document->createElement('externalReferences'),
$refs
);
}

private function normalizeDependencies(Bom $bom): ?DOMElement
{
$factory = $this->getNormalizerFactory();
$factory = $this->normalizerFactory;

if (false === $factory->getSpec()->supportsDependencies()) {
if (false === $factory->spec->supportsDependencies()) {
return null;
}

Expand All @@ -139,22 +138,22 @@ private function normalizeDependencies(Bom $bom): ?DOMElement
return empty($deps)
? null
: SimpleDOM::appendChildren(
$factory->getDocument()->createElement('dependencies'),
$factory->document->createElement('dependencies'),
$deps
);
}

private function normalizeProperties(PropertyRepository $properties): ?DOMElement
{
if (false === $this->getNormalizerFactory()->getSpec()->supportsBomProperties(Format::XML)) {
if (false === $this->normalizerFactory->spec->supportsBomProperties(Format::XML)) {
return null;
}

return 0 === \count($properties)
? null
: SimpleDOM::appendChildren(
$this->getNormalizerFactory()->getDocument()->createElement('properties'),
$this->getNormalizerFactory()->makeForPropertyRepository()->normalize($properties)
$this->normalizerFactory->document->createElement('properties'),
$this->normalizerFactory->makeForPropertyRepository()->normalize($properties)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class ComponentEvidenceNormalizer extends _BaseNormalizer
{
public function normalize(ComponentEvidence $evidence): DOMElement
{
$factory = $this->getNormalizerFactory();
$document = $factory->getDocument();
$document = $this->normalizerFactory->document;

$licenses = $evidence->getLicenses();
$copyright = $evidence->getCopyright();
Expand All @@ -48,7 +47,7 @@ public function normalize(ComponentEvidence $evidence): DOMElement
? null
: SimpleDOM::appendChildren(
$document->createElement('licenses'),
$this->getNormalizerFactory()->makeForLicenseRepository()->normalize($licenses)
$this->normalizerFactory->makeForLicenseRepository()->normalize($licenses)
),
0 === \count($copyright)
? null
Expand Down
27 changes: 13 additions & 14 deletions src/Core/Serialization/DOM/Normalizers/ComponentNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public function normalize(Component $component): DOMElement
$group = $component->getGroup();
$version = $component->getVersion();

$factory = $this->getNormalizerFactory();
$spec = $factory->getSpec();
$spec = $this->normalizerFactory->spec;

$type = $component->getType();
if (false === $spec->isSupportedComponentType($type)) {
Expand All @@ -68,7 +67,7 @@ public function normalize(Component $component): DOMElement
? $component->getEvidence()
: null;

$document = $factory->getDocument();
$document = $this->normalizerFactory->document;

return SimpleDOM::appendChildren(
SimpleDOM::setAttributes(
Expand Down Expand Up @@ -106,31 +105,31 @@ public function normalize(Component $component): DOMElement
// components
null === $evidence
? null
: $this->getNormalizerFactory()->makeForComponentEvidence()->normalize($evidence),
: $this->normalizerFactory->makeForComponentEvidence()->normalize($evidence),
]
);
}

private function normalizeLicenses(LicenseRepository $licenses): ?DOMElement
{
$factory = $this->getNormalizerFactory();
$factory = $this->normalizerFactory;

return 0 === \count($licenses)
? null
: SimpleDOM::appendChildren(
$factory->getDocument()->createElement('licenses'),
$factory->document->createElement('licenses'),
$factory->makeForLicenseRepository()->normalize($licenses)
);
}

private function normalizeHashes(HashDictionary $hashes): ?DOMElement
{
$factory = $this->getNormalizerFactory();
$factory = $this->normalizerFactory;

return 0 === \count($hashes)
? null
: SimpleDOM::appendChildren(
$factory->getDocument()->createElement('hashes'),
$factory->document->createElement('hashes'),
$factory->makeForHashDictionary()->normalize($hashes)
);
}
Expand All @@ -140,35 +139,35 @@ private function normalizePurl(?PackageUrl $purl): ?DOMElement
return null === $purl
? null
: SimpleDOM::makeSafeTextElement(
$this->getNormalizerFactory()->getDocument(),
$this->normalizerFactory->document,
'purl',
XML::encodeAnyUriBE((string) $purl)
);
}

private function normalizeExternalReferences(ExternalReferenceRepository $extRefs): ?DOMElement
{
$factory = $this->getNormalizerFactory();
$factory = $this->normalizerFactory;

return 0 === \count($extRefs)
? null
: SimpleDOM::appendChildren(
$factory->getDocument()->createElement('externalReferences'),
$factory->document->createElement('externalReferences'),
$factory->makeForExternalReferenceRepository()->normalize($extRefs)
);
}

private function normalizeProperties(PropertyRepository $properties): ?DOMElement
{
if (false === $this->getNormalizerFactory()->getSpec()->supportsComponentProperties()) {
if (false === $this->normalizerFactory->spec->supportsComponentProperties()) {
return null;
}

return 0 === \count($properties)
? null
: SimpleDOM::appendChildren(
$this->getNormalizerFactory()->getDocument()->createElement('properties'),
$this->getNormalizerFactory()->makeForPropertyRepository()->normalize($properties)
$this->normalizerFactory->document->createElement('properties'),
$this->normalizerFactory->makeForPropertyRepository()->normalize($properties)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ComponentRepositoryNormalizer extends _BaseNormalizer
*/
public function normalize(ComponentRepository $repo): array
{
$normalizer = $this->getNormalizerFactory()->makeForComponent();
$normalizer = $this->normalizerFactory->makeForComponent();

$components = [];
foreach ($repo->getItems() as $component) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private function normalizeDependency(BomRef $componentRef, BomRef ...$dependency
return null;
}

$doc = $this->getNormalizerFactory()->getDocument();
$doc = $this->normalizerFactory->document;

$dependency = SimpleDOM::setAttributes(
$doc->createElement('dependency'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public function normalize(ExternalReference $externalReference): DOMElement
$anyURI = XML::encodeAnyUriBE($refURI)
?? throw new UnexpectedValueException("unable to make 'anyURI' from: $refURI");

$factory = $this->getNormalizerFactory();
$spec = $factory->getSpec();
$spec = $this->normalizerFactory->spec;

$type = $externalReference->getType();
if (false === $spec->isSupportedExternalReferenceType($type)) {
Expand All @@ -60,7 +59,7 @@ public function normalize(ExternalReference $externalReference): DOMElement
}
}

$doc = $factory->getDocument();
$doc = $this->normalizerFactory->document;

return SimpleDOM::appendChildren(
SimpleDOM::setAttributes(
Expand All @@ -83,13 +82,13 @@ private function normalizeHashes(HashDictionary $hashes): ?DOMElement
return null;
}

$factory = $this->getNormalizerFactory();
if (false === $factory->getSpec()->supportsExternalReferenceHashes()) {
$factory = $this->normalizerFactory;
if (false === $factory->spec->supportsExternalReferenceHashes()) {
return null;
}

return SimpleDOM::appendChildren(
$factory->getDocument()->createElement('hashes'),
$factory->document->createElement('hashes'),
$factory->makeForHashDictionary()->normalize($hashes)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ExternalReferenceRepositoryNormalizer extends _BaseNormalizer
*/
public function normalize(ExternalReferenceRepository $repo): array
{
$normalizer = $this->getNormalizerFactory()->makeForExternalReference();
$normalizer = $this->normalizerFactory->makeForExternalReference();

$externalReferences = [];
foreach ($repo->getItems() as $externalReference) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function normalize(HashDictionary $repo): array
{
$hashes = [];

$hashNormalizer = $this->getNormalizerFactory()->makeForHash();
$hashNormalizer = $this->normalizerFactory->makeForHash();
foreach ($repo->getItems() as [$algorithm , $content]) {
try {
$hashes[] = $hashNormalizer->normalize($algorithm, $content);
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Serialization/DOM/Normalizers/HashNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class HashNormalizer extends _BaseNormalizer
*/
public function normalize(HashAlgorithm $algorithm, string $content): DOMElement
{
$spec = $this->getNormalizerFactory()->getSpec();
$spec = $this->normalizerFactory->spec;
if (false === $spec->isSupportedHashAlgorithm($algorithm)) {
throw new DomainException("Invalid hash algorithm: $algorithm->name", 1);
}
Expand All @@ -48,7 +48,7 @@ public function normalize(HashAlgorithm $algorithm, string $content): DOMElement
}

$element = SimpleDOM::makeSafeTextElement(
$this->getNormalizerFactory()->getDocument(),
$this->normalizerFactory->document,
'hash',
$content
);
Expand Down
Loading