Skip to content

Commit

Permalink
code adjust
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <[email protected]>
  • Loading branch information
jkowalleck committed Mar 10, 2023
1 parent 5f33dbc commit 00de098
Show file tree
Hide file tree
Showing 31 changed files with 107 additions and 108 deletions.
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
10 changes: 5 additions & 5 deletions src/Core/Serialization/DOM/Normalizers/LicenseNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public function normalize(LicenseExpression|SpdxLicense|NamedLicense $license):
private function normalizeExpression(LicenseExpression $license): DOMElement
{
// TODO: IMPLEMENTED IF NEEDED: may throw, if not supported by the spec
// $this->getNormalizerFactory()->getSpec()->supportsLicenseExpression()
// $this->normalizerFactory->spec->supportsLicenseExpression()

$element = SimpleDOM::makeSafeTextElement(
$this->getNormalizerFactory()->getDocument(),
$this->normalizerFactory->document,
'expression',
$license->getExpression()
);
Expand All @@ -63,17 +63,17 @@ private function normalizeExpression(LicenseExpression $license): DOMElement
*/
private function normalizeDisjunctive(SpdxLicense|NamedLicense $license): DOMElement
{
$factory = $this->getNormalizerFactory();
$factory = $this->normalizerFactory;

[$id, $name] = $license instanceof SpdxLicense
? [$license->getId(), null]
: [null, $license->getName()];

if (null !== $id && !$factory->getSpec()->isSupportedLicenseIdentifier($id)) {
if (null !== $id && !$factory->spec->isSupportedLicenseIdentifier($id)) {
[$id, $name] = [null, $id];
}

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

return SimpleDOM::appendChildren(
$document->createElement('license'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class LicenseRepositoryNormalizer extends _BaseNormalizer
public function normalize(LicenseRepository $repo): array
{
return array_map(
$this->getNormalizerFactory()->makeForLicense()->normalize(...),
$this->normalizerFactory->makeForLicense()->normalize(...),
$repo->getItems()
);
}
Expand Down
Loading

0 comments on commit 00de098

Please sign in to comment.