Skip to content

Commit

Permalink
Trying to remove public constructors (#12)
Browse files Browse the repository at this point in the history
* Trying to remove public constructors

* Fix PHPUnit minimum version
See https://stackoverflow.com/questions/70321515/catching-warnings-notices-and-deprecations-with-phpunit-9-4-on-php-8-1-0

* Increased Mutation limits
  • Loading branch information
Spomky authored Aug 22, 2022
1 parent 3e61cb6 commit 4dc75ff
Show file tree
Hide file tree
Showing 318 changed files with 2,932 additions and 2,713 deletions.
2 changes: 1 addition & 1 deletion .github/stale.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
daysUntilStale: 60
daysUntilStale: 30
daysUntilClose: 7
staleLabel: wontfix
markComment: >
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ st: vendor ## Run static analyse
################################################

ci-mu: vendor ## Mutation tests (for Github only)
vendor/bin/infection --logger-github -s --threads=$(nproc) --min-msi=40 --min-covered-msi=40
vendor/bin/infection --logger-github -s --threads=$(nproc) --min-msi=80 --min-covered-msi=80

.PHONY: ci-cc
ci-cc: vendor ## Show test coverage rates (for CI/CD only)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.1",
"phpstan/phpstan-strict-rules": "^1.3",
"phpunit/phpunit": "^9.0",
"phpunit/phpunit": "^9.5.5",
"rector/rector": "^0.14",
"roave/security-advisories": "dev-latest",
"symfony/phpunit-bridge": "^6.1",
Expand Down
4 changes: 2 additions & 2 deletions src/ASN1/Component/Identifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static function fromDER(string $data, int &$offset = null): self
$tag = (0b00011111 & $byte);
// long-form identifier
if ($tag === 0x1f) {
$tag = self::_decodeLongFormTag($data, $idx);
$tag = self::decodeLongFormTag($data, $idx);
}
if (isset($offset)) {
$offset = $idx;
Expand Down Expand Up @@ -256,7 +256,7 @@ public static function classToName(int $class): string
*
* @return BigInteger Tag number
*/
private static function _decodeLongFormTag(string $data, int &$offset): BigInteger
private static function decodeLongFormTag(string $data, int &$offset): BigInteger
{
$datalen = mb_strlen($data, '8bit');
$tag = BigInteger::of(0);
Expand Down
4 changes: 2 additions & 2 deletions src/ASN1/Component/Length.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static function fromDER(string $data, int &$offset = null): self
if ($idx + $length > $datalen) {
throw new DecodeException('Unexpected end of data while decoding long form length.');
}
$length = self::_decodeLongFormLength($length, $data, $idx);
$length = self::decodeLongFormLength($length, $data, $idx);
}
}
if (isset($offset)) {
Expand Down Expand Up @@ -186,7 +186,7 @@ public function isIndefinite(): bool
* @param string $data Data
* @param int $offset reference to the variable containing offset to the data
*/
private static function _decodeLongFormLength(int $length, string $data, int &$offset): BigInteger
private static function decodeLongFormLength(int $length, string $data, int &$offset): BigInteger
{
// first octet must not be 0xff (spec 8.1.3.5c)
if ($length === 127) {
Expand Down
24 changes: 12 additions & 12 deletions src/ASN1/DERData.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ final class DERData extends Element
/**
* DER encoded data.
*/
protected string $_der;
private readonly string $der;

/**
* Identifier of the underlying type.
*/
protected Identifier $_identifier;
private readonly Identifier $identifier;

/**
* Offset to the content in DER data.
*/
protected int $_contentOffset = 0;
private int $contentOffset = 0;

/**
* @param string $data DER encoded data
*/
private function __construct(string $data)
{
$this->_identifier = Identifier::fromDER($data, $this->_contentOffset);
$this->identifier = Identifier::fromDER($data, $this->contentOffset);
// check that length encoding is valid
Length::expectFromDER($data, $this->_contentOffset);
$this->_der = $data;
parent::__construct($this->_identifier->intTag());
Length::expectFromDER($data, $this->contentOffset);
$this->der = $data;
parent::__construct($this->identifier->intTag());
}

public static function create(string $data): self
Expand All @@ -51,26 +51,26 @@ public static function create(string $data): self

public function typeClass(): int
{
return $this->_identifier->typeClass();
return $this->identifier->typeClass();
}

public function isConstructed(): bool
{
return $this->_identifier->isConstructed();
return $this->identifier->isConstructed();
}

public function toDER(): string
{
return $this->_der;
return $this->der;
}

protected function encodedAsDER(): string
{
// if there's no content payload
if (mb_strlen($this->_der, '8bit') === $this->_contentOffset) {
if (mb_strlen($this->der, '8bit') === $this->contentOffset) {
return '';
}
return mb_substr($this->_der, $this->_contentOffset, null, '8bit');
return mb_substr($this->der, $this->contentOffset, null, '8bit');
}

protected static function decodeFromDER(Identifier $identifier, string $data, int &$offset): ElementBase
Expand Down
36 changes: 18 additions & 18 deletions src/ASN1/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ abstract class Element implements ElementBase
];

/**
* @param bool $_indefiniteLength Whether type shall be encoded with indefinite length.
* @param bool $indefiniteLength Whether type shall be encoded with indefinite length.
*/
protected function __construct(
protected int $typeTag,
protected bool $_indefiniteLength = false
protected readonly int $typeTag,
protected bool $indefiniteLength = false
) {
}

Expand All @@ -245,7 +245,7 @@ public static function fromDER(string $data, int &$offset = null): static
// decode identifier
$identifier = Identifier::fromDER($data, $idx);
// determine class that implements type specific decoding
$cls = self::_determineImplClass($identifier);
$cls = self::determineImplClass($identifier);
// decode remaining element
$element = $cls::decodeFromDER($identifier, $data, $idx);
// if called in the context of a concrete class, check
Expand All @@ -271,7 +271,7 @@ public function toDER(): string
$this->typeTag
);
$content = $this->encodedAsDER();
if ($this->_indefiniteLength) {
if ($this->indefiniteLength) {
$length = Length::create(0, true);
$eoc = EOC::create();
return $identifier->toDER() . $length->toDER() . $content . $eoc->toDER();
Expand All @@ -293,16 +293,16 @@ public function isType(int $tag): bool
}
// negative tags identify an abstract pseudotype
if ($tag < 0) {
return $this->_isPseudoType($tag);
return $this->isPseudoType($tag);
}
return $this->_isConcreteType($tag);
return $this->isConcreteType($tag);
}

public function expectType(int $tag): ElementBase
{
if (! $this->isType($tag)) {
throw new UnexpectedValueException(
sprintf('%s expected, got %s.', self::tagToName($tag), $this->_typeDescriptorString())
sprintf('%s expected, got %s.', self::tagToName($tag), $this->typeDescriptorString())
);
}
return $this;
Expand Down Expand Up @@ -331,7 +331,7 @@ public function expectTagged(?int $tag = null): TaggedType
*/
public function hasIndefiniteLength(): bool
{
return $this->_indefiniteLength;
return $this->indefiniteLength;
}

/**
Expand All @@ -342,7 +342,7 @@ public function hasIndefiniteLength(): bool
public function withIndefiniteLength(bool $indefinite = true): self
{
$obj = clone $this;
$obj->_indefiniteLength = $indefinite;
$obj->indefiniteLength = $indefinite;
return $obj;
}

Expand Down Expand Up @@ -391,11 +391,11 @@ abstract protected static function decodeFromDER(Identifier $identifier, string
*
* @return string Class name
*/
protected static function _determineImplClass(Identifier $identifier): string
protected static function determineImplClass(Identifier $identifier): string
{
switch ($identifier->typeClass()) {
case Identifier::CLASS_UNIVERSAL:
$cls = self::_determineUniversalImplClass($identifier->intTag());
$cls = self::determineUniversalImplClass($identifier->intTag());
// constructed strings may be present in BER
if ($identifier->isConstructed()
&& is_subclass_of($cls, StringType::class)) {
Expand All @@ -421,7 +421,7 @@ protected static function _determineImplClass(Identifier $identifier): string
*
* @return string Class name
*/
protected static function _determineUniversalImplClass(int $tag): string
protected static function determineUniversalImplClass(int $tag): string
{
if (! array_key_exists($tag, self::MAP_TAG_TO_CLASS)) {
throw new UnexpectedValueException("Universal tag {$tag} not implemented.");
Expand All @@ -432,7 +432,7 @@ protected static function _determineUniversalImplClass(int $tag): string
/**
* Get textual description of the type for debugging purposes.
*/
protected function _typeDescriptorString(): string
protected function typeDescriptorString(): string
{
if ($this->typeClass() === Identifier::CLASS_UNIVERSAL) {
return self::tagToName($this->typeTag);
Expand All @@ -441,17 +441,17 @@ protected function _typeDescriptorString(): string
}

/**
* Check whether the element is a concrete type of a given tag.
* Check whether the element is a concrete type of given tag.
*/
private function _isConcreteType(int $tag): bool
private function isConcreteType(int $tag): bool
{
// if tag doesn't match
if ($this->tag() !== $tag) {
return false;
}
// if type is universal check that instance is of a correct class
if ($this->typeClass() === Identifier::CLASS_UNIVERSAL) {
$cls = self::_determineUniversalImplClass($tag);
$cls = self::determineUniversalImplClass($tag);
if (! $this instanceof $cls) {
return false;
}
Expand All @@ -462,7 +462,7 @@ private function _isConcreteType(int $tag): bool
/**
* Check whether the element is a pseudotype.
*/
private function _isPseudoType(int $tag): bool
private function isPseudoType(int $tag): bool
{
return match ($tag) {
self::TYPE_STRING => $this instanceof StringType,
Expand Down
17 changes: 11 additions & 6 deletions src/ASN1/Type/BaseString.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ abstract class BaseString extends Element implements StringType, Stringable
/**
* String value.
*/
protected string $_string;
private readonly string $string;

public function __construct(int $typeTag, string $string)
protected function __construct(int $typeTag, string $string)
{
parent::__construct($typeTag);
if (! $this->_validateString($string)) {
if (! $this->validateString($string)) {
throw new InvalidArgumentException(sprintf('Not a valid %s string.', self::tagToName($this->typeTag)));
}
$this->_string = $string;
$this->string = $string;
}

public function __toString(): string
Expand All @@ -37,13 +37,18 @@ public function __toString(): string
*/
public function string(): string
{
return $this->_string;
return $this->string;
}

protected function encodedAsDER(): string
{
return $this->string;
}

/**
* Check whether string is valid for the concrete type.
*/
protected function _validateString(string $string): bool
protected function validateString(string $string): bool
{
// Override in derived classes
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/ASN1/Type/BaseTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ abstract class BaseTime extends Element implements TimeType, Stringable
*/
public const TZ_UTC = 'UTC';

public function __construct(
protected function __construct(
int $typeTag,
protected readonly DateTimeImmutable $_dateTime
protected readonly DateTimeImmutable $dateTime
) {
parent::__construct($typeTag);
}
Expand All @@ -46,7 +46,7 @@ abstract public static function fromString(string $time): static;
*/
public function dateTime(): DateTimeImmutable
{
return $this->_dateTime;
return $this->dateTime;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ASN1/Type/Constructed/ConstructedString.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ protected static function decodeIndefiniteLength(int $typeTag, string $data, int
}
$offset = $idx;
$type = self::createWithTag($typeTag, ...$elements);
$type->_indefiniteLength = true;
$type->indefiniteLength = true;
return $type;
}
}
2 changes: 1 addition & 1 deletion src/ASN1/Type/Constructed/Sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected static function decodeIndefiniteLength(string $data, int &$offset): se
}
$offset = $idx;
$type = self::create(...$elements);
$type->_indefiniteLength = true;
$type->indefiniteLength = true;
return $type;
}
}
2 changes: 1 addition & 1 deletion src/ASN1/Type/Constructed/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected static function decodeIndefiniteLength(string $data, int &$offset): El
}
$offset = $idx;
$type = self::create(...$elements);
$type->_indefiniteLength = true;
$type->indefiniteLength = true;
return $type;
}
}
2 changes: 1 addition & 1 deletion src/ASN1/Type/Primitive/BMPString.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function create(string $string): self
return new self($string);
}

protected function _validateString(string $string): bool
protected function validateString(string $string): bool
{
// UCS-2 has fixed with of 2 octets (16 bits)
return mb_strlen($string, '8bit') % 2 === 0;
Expand Down
Loading

0 comments on commit 4dc75ff

Please sign in to comment.