Skip to content

Commit

Permalink
Merge pull request #15 from renfordt/phpstan
Browse files Browse the repository at this point in the history
Fixed potential bugs with PHPStan
  • Loading branch information
renfordt authored Dec 14, 2024
2 parents 306a9c3 + 581c5d4 commit f127330
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function getBackgroundLightness(): float
*/
public function setBackgroundLightness(float $backgroundLightness): void
{
$this->backgroundLightness = clamp($backgroundLightness, 0, 1);
$this->backgroundLightness = (float)clamp($backgroundLightness, 0, 1);
}

/**
Expand All @@ -159,7 +159,7 @@ public function getTextLightness(): float
*/
public function setTextLightness(float $textLightness): void
{
$this->textLightness = clamp($textLightness, 0, 1);
$this->textLightness = (float)clamp($textLightness, 0, 1);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Identicon.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function getSVG(): string
/**
* Generate a symmetric identicon matrix based on the provided hash
*
* @return array The generated symmetric matrix
* @return array<int, array<int|string, bool>> The generated symmetric matrix
*/
public function generateSymmetricMatrix(): array
{
Expand All @@ -125,7 +125,7 @@ public function generateSymmetricMatrix(): array
/**
* Returns the symmetry matrix.
*
* @return array The symmetry matrix.
* @return array<int<0, max>, list<int>> The symmetry matrix.
*/
private function getSymmetryMatrix(): array
{
Expand Down Expand Up @@ -155,7 +155,7 @@ private function convertStrToBool(string $char): bool
* Generates a matrix based on the given offset value.
*
* @param int $offset The offset value for generating the matrix. Defaults to 0.
* @return array The generated matrix.
* @return array<int<0, max>, array<int, bool>> The generated matrix.
*/
private function generateMatrix(int $offset = 0): array
{
Expand Down
38 changes: 18 additions & 20 deletions src/InitialsAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class InitialsAvatar extends Avatar
/**
* Constructs a new instance of the class.
*
* @param Name $name The name object to set
* @param Name $name The name object to set
*
* @return void
*/
Expand All @@ -46,7 +46,7 @@ public static function make(Name|string $name): InitialsAvatar
/**
* Sets the form of the application
*
* @param string|FormTypes $form The form type
* @param string|FormTypes $form The form type
* @return void
*/
public function setForm(string|FormTypes $form): void
Expand All @@ -61,7 +61,7 @@ public function setForm(string|FormTypes $form): void
/**
* Sets the rotation angle of the element
*
* @param int $angle The rotation angle value
* @param int $angle The rotation angle value
*
* @return void
*/
Expand All @@ -83,7 +83,7 @@ public function getOffset(): int
/**
* Sets the offset for the avatar
*
* @param int $offset The offset in pixel
* @param int $offset The offset in pixel
* @return void
*/
public function setOffset(int $offset): void
Expand All @@ -94,7 +94,7 @@ public function setOffset(int $offset): void
/**
* Returns the HTML representation of the code.
*
* @param bool $base64 Determines if the image source should be in base64 format. Default is false.
* @param bool $base64 Determines if the image source should be in base64 format. Default is false.
* @return string The HTML representation of the code.
*/
public function getHTML(bool $base64 = false): string
Expand All @@ -103,7 +103,7 @@ public function getHTML(bool $base64 = false): string
return $this->generate();
}

return '<img src="'.$this->getBase64().'" />';
return '<img src="' . $this->getBase64() . '" />';
}

/**
Expand Down Expand Up @@ -139,9 +139,7 @@ public function generate(): string

$initials = $this->getInitials($darkColor);

if (isset($outlineForm)) {
$doc->addChild($outlineForm);
}
$doc->addChild($outlineForm);
$doc->addChild($initials);

return $larvatar;
Expand All @@ -154,15 +152,15 @@ public function generate(): string
private function addFontIfNotEmpty(): void
{
if ($this->fontPath != '' && $this->fontFamily != '') {
SVG::addFont(__DIR__.$this->fontPath);
SVG::addFont(__DIR__ . $this->fontPath);
}
}

/**
* Get a circle SVG element
*
* @param float $halfSize Half of the size of the circle
* @param HSLColor $lightColor The light color to fill the circle with
* @param float $halfSize Half of the size of the circle
* @param HSLColor $lightColor The light color to fill the circle with
* @return SVGCircle The circle SVG element
*/
private function getCircle(float $halfSize, HSLColor $lightColor): SVGCircle
Expand All @@ -176,8 +174,8 @@ private function getCircle(float $halfSize, HSLColor $lightColor): SVGCircle
/**
* Get a square SVGRect
*
* @param float $size Half of the square size
* @param HSLColor $lightColor The color of the square
* @param float $size Half of the square size
* @param HSLColor $lightColor The color of the square
*
* @return SVGRect The generated square SVGRect object
*/
Expand All @@ -191,9 +189,9 @@ private function getSquare(float $size, HSLColor $lightColor): SVGRect
/**
* Get a polygon shape
*
* @param float $size The size of the polygon
* @param HSLColor $lightColor The light color to fill the polygon
* @param int $rotation
* @param float $size The size of the polygon
* @param HSLColor $lightColor The light color to fill the polygon
* @param int $rotation
* @return SVGPolygon The polygon shape with the specified size and color
*/
private function getHexagon(float $size, HSLColor $lightColor, int $rotation = 0): SVGPolygon
Expand Down Expand Up @@ -232,15 +230,15 @@ private function getInitials(HSLColor $darkColor): SVGText
if ($this->fontSize == 0) {
$this->fontSize = $this->calculateFontSize($initialsText);
}
$initials->setFontSize($this->fontSize.'px');
$initials->setFontSize($this->fontSize . 'px');

return $initials;
}

/**
* Calculate the font size based on the initials length
*
* @param string $initials The initials to calculate the font size for
* @param string $initials The initials to calculate the font size for
* @return int The calculated font size
*/
protected function calculateFontSize(string $initials): int
Expand All @@ -255,6 +253,6 @@ protected function calculateFontSize(string $initials): int
*/
public function getBase64(): string
{
return 'data:image/svg+xml;base64,'.base64_encode($this->generate());
return 'data:image/svg+xml;base64,' . base64_encode($this->generate());
}
}
24 changes: 12 additions & 12 deletions src/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Name
*/
private string $name = '';
/**
* @var array $splitNames An array to store split names
* @var array<string> $splitNames An array to store split names
*/
private array $splitNames;
/**
Expand All @@ -37,25 +37,25 @@ private function hash(): string
}

/**
* Get the hexadecimal color value
* Create an instance of the Name class.
*
* @param int $offset The starting offset for the substring
* @return HexColor The hexadecimal color string
* @param string $name The name to be used for creating the Name object.
* @return Name The newly created Name object.
*/
public function getHexColor(int $offset = 0): HexColor
public static function make(string $name): Name
{
return HexColor::create('#'.substr($this->hash, $offset, 6));
return new Name($name);
}

/**
* Create an instance of the Name class.
* Get the hexadecimal color value
*
* @param string $name The name to be used for creating the Name object.
* @return Name The newly created Name object.
* @param int $offset The starting offset for the substring
* @return HexColor The hexadecimal color string
*/
public static function make(string $name): Name
public function getHexColor(int $offset = 0): HexColor
{
return new Name($name);
return HexColor::create('#' . substr($this->hash, $offset, 6));
}

/**
Expand Down Expand Up @@ -96,7 +96,7 @@ public function getInitials(): string
/**
* Retrieves the split names.
*
* @return array The split names.
* @return array<string> The split names.
*/
public function getSplitNames(): array
{
Expand Down
8 changes: 4 additions & 4 deletions src/Traits/ColorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ trait ColorTrait
/**
* Get the color set for a given name.
*
* @param Name $name The name to get the color set for.
* @param float $textLightness The lightness value for the text color. Default is 0.35.
* @param float $backgroundLightness The lightness value for the background color. Default is 0.8.
* @return array An array containing the dark and light colors in HSL format.
* @param Name $name The name to get the color set for.
* @param float $textLightness The lightness value for the text color. Default is 0.35.
* @param float $backgroundLightness The lightness value for the background color. Default is 0.8.
* @return array<HSLColor> An array containing the dark and light colors in HSL format.
*/
public function getColorSet(Name $name, float $textLightness = 0.35, float $backgroundLightness = 0.8): array
{
Expand Down

0 comments on commit f127330

Please sign in to comment.