Improved typing and phpdoc type annotations

This commit is contained in:
Jan Böhmer 2023-06-18 15:37:42 +02:00
parent 3817ba774d
commit b7c8ca2a48
39 changed files with 189 additions and 129 deletions

View file

@ -33,8 +33,8 @@ use Symfony\Component\Filesystem\Filesystem;
*/
class AttachmentPathResolver
{
protected ?string $media_path;
protected ?string $footprints_path;
protected string $media_path;
protected string $footprints_path;
protected ?string $models_path;
protected ?string $secure_path;
@ -54,11 +54,11 @@ class AttachmentPathResolver
*/
public function __construct(protected string $project_dir, string $media_path, string $secure_path, ?string $footprints_path, ?string $models_path)
{
//Determine the path for our ressources
$this->media_path = $this->parameterToAbsolutePath($media_path);
$this->footprints_path = $this->parameterToAbsolutePath($footprints_path);
//Determine the path for our resources
$this->media_path = $this->parameterToAbsolutePath($media_path) ?? throw new \InvalidArgumentException('The media path must be set and valid!');
$this->secure_path = $this->parameterToAbsolutePath($secure_path) ?? throw new \InvalidArgumentException('The secure path must be set and valid!');
$this->footprints_path = $this->parameterToAbsolutePath($footprints_path) ;
$this->models_path = $this->parameterToAbsolutePath($models_path);
$this->secure_path = $this->parameterToAbsolutePath($secure_path);
$this->pathes = [$this->media_path, $this->media_path, $this->footprints_path, $this->models_path, $this->secure_path];
//Remove all disabled placeholders
@ -192,7 +192,7 @@ class AttachmentPathResolver
}
/**
* The path where uploaded attachments is stored.
* The path where uploaded attachments is stored.
*
* @return string the absolute path to the media folder
*/
@ -202,8 +202,8 @@ class AttachmentPathResolver
}
/**
* The path where secured attachments are stored. Must not be located in public/ folder, so it can only be accessed
* via the attachment controller.
* The path where secured attachments are stored. Must not be located in public/ folder, so it can only be accessed
* via the attachment controller.
*
* @return string the absolute path to the secure path
*/
@ -215,7 +215,7 @@ class AttachmentPathResolver
/**
* The string where the builtin footprints are stored.
*
* @return string|null The absolute path to the footprints folder. Null if built footprints were disabled.
* @return string|null The absolute path to the footprints' folder. Null if built footprints were disabled.
*/
public function getFootprintsPath(): ?string
{
@ -225,7 +225,7 @@ class AttachmentPathResolver
/**
* The string where the builtin 3D models are stored.
*
* @return string|null The absolute path to the models folder. Null if builtin models were disabled.
* @return string|null The absolute path to the models' folder. Null if builtin models were disabled.
*/
public function getModelsPath(): ?string
{

View file

@ -159,7 +159,7 @@ class EntityURLGenerator
public function viewURL(Attachment $entity): string
{
if ($entity->isExternal()) { //For external attachments, return the link to external path
return $entity->getURL();
return $entity->getURL() ?? throw new \RuntimeException('External attachment has no URL!');
}
//return $this->urlGenerator->generate('attachment_view', ['id' => $entity->getID()]);
return $this->attachmentURLGenerator->getViewURL($entity) ?? '';
@ -169,7 +169,7 @@ class EntityURLGenerator
{
if ($entity instanceof Attachment) {
if ($entity->isExternal()) { //For external attachments, return the link to external path
return $entity->getURL();
return $entity->getURL() ?? throw new \RuntimeException('External attachment has no URL!');
}
return $this->attachmentURLGenerator->getDownloadURL($entity);

View file

@ -60,7 +60,9 @@ final class LabelGenerator
}
/**
* @param object|object[] $elements An element or an array of elements for which labels should be generated
* @param object|object[] $elements An element or an array of elements for which labels should be generated
*
* @return null|string
*/
public function generateLabel(LabelOptions $options, object|array $elements): string
{
@ -83,7 +85,7 @@ final class LabelGenerator
$dompdf->loadHtml($this->labelHTMLGenerator->getLabelHTML($options, $elements));
$dompdf->render();
return $dompdf->output();
return $dompdf->output() ?? throw new \RuntimeException('Could not generate label!');
}
/**

View file

@ -77,19 +77,20 @@ final class LabelTextReplacer
}
/**
* Replaces all placeholders in the input lines.
* Replaces all placeholders in the input lines.
*
* @param string $lines The input lines that should be replaced
* @param object $target the object that should be used as source for the informations
* @param object $target the object that should be used as source for the information
*
* @return string the Lines with replaced informations
* @return string the Lines with replaced information
*/
public function replace(string $lines, object $target): string
{
$patterns = [
'/(\[\[[A-Z_0-9]+\]\])/' => fn($match) => $this->handlePlaceholder($match[0], $target),
'/(\[\[[A-Z_0-9]+\]\])/' => fn($match): string => $this->handlePlaceholder($match[0], $target),
];
return preg_replace_callback_array($patterns, $lines);
return preg_replace_callback_array($patterns, $lines) ?? throw new \RuntimeException('Could not replace placeholders!');
}
}

View file

@ -55,10 +55,10 @@ class HistoryHelper
}
/**
* Returns an array containing all elements that are associated with the argument.
* The returned array contains the given element.
* Returns an array containing all elements that are associated with the argument.
* The returned array contains the given element.
*
* @psalm-return array<AbstractParameter|array-key, mixed>
* @return AbstractDBElement[]
*/
public function getAssociatedElements(AbstractDBElement $element): array
{

View file

@ -57,7 +57,11 @@ class LogDiffFormatter
]);
}
private function diffNumeric($old_data, $new_data): string
/**
* @param numeric $old_data
* @param numeric $new_data
*/
private function diffNumeric(int|float|string $old_data, int|float|string $new_data): string
{
if ((!is_numeric($old_data)) || (!is_numeric($new_data))) {
throw new \InvalidArgumentException('The given data is not numeric.');

View file

@ -232,16 +232,17 @@ class TimeTravel
{
$reflection = new ReflectionClass($element::class);
$property = $reflection->getProperty($field);
$property->setAccessible(true);
return $property->getValue($element);
}
/**
* @param int|null|object $new_value
*/
protected function setField(AbstractDBElement $element, string $field, mixed $new_value): void
{
$reflection = new ReflectionClass($element::class);
$property = $reflection->getProperty($field);
$property->setAccessible(true);
$property->setValue($element, $new_value);
}

View file

@ -178,7 +178,7 @@ implode(',', array_map(static fn (PartLot $lot) => $lot->getID(), $part->getPart
*
* @throws AccessDeniedException
*/
private function denyAccessUnlessGranted($attributes, $subject = null, string $message = 'Access Denied.'): void
private function denyAccessUnlessGranted(mixed $attributes, mixed $subject = null, string $message = 'Access Denied.'): void
{
if (!$this->security->isGranted($attributes, $subject)) {
$exception = new AccessDeniedException($message);

View file

@ -66,12 +66,16 @@ class NodesListBuilder
}
/**
* Returns a flattened list of all (recursive) children elements of the given AbstractStructuralDBElement.
* The value is cached for performance reasons.
* Returns a flattened list of all (recursive) children elements of the given AbstractStructuralDBElement.
* The value is cached for performance reasons.
*
* @template T of AbstractStructuralDBElement
* @param T $element
* @return T[]
*
* @param T $element
*
* @return AbstractStructuralDBElement[]
*
* @phpstan-return list<T>
*/
public function getChildrenFlatList(AbstractStructuralDBElement $element): array
{

View file

@ -42,13 +42,16 @@ class UserAvatarHelper
/**
* Returns the URL to the profile picture of the given user (in big size)
* Returns the URL to the profile picture of the given user (in big size)
*
* @return string
*/
public function getAvatarURL(User $user): string
{
//Check if the user has a master attachment defined (meaning he has explicitly defined a profile picture)
if ($user->getMasterPictureAttachment() instanceof Attachment) {
return $this->attachmentURLGenerator->getThumbnailURL($user->getMasterPictureAttachment(), 'thumbnail_md');
return $this->attachmentURLGenerator->getThumbnailURL($user->getMasterPictureAttachment(), 'thumbnail_md')
?? throw new RuntimeException('Could not generate thumbnail URL');
}
//If not check if gravatar is enabled (then use gravatar URL)
@ -64,7 +67,8 @@ class UserAvatarHelper
{
//Check if the user has a master attachment defined (meaning he has explicitly defined a profile picture)
if ($user->getMasterPictureAttachment() instanceof Attachment) {
return $this->attachmentURLGenerator->getThumbnailURL($user->getMasterPictureAttachment(), 'thumbnail_xs');
return $this->attachmentURLGenerator->getThumbnailURL($user->getMasterPictureAttachment(), 'thumbnail_xs')
?? throw new RuntimeException('Could not generate thumbnail URL');;
}
//If not check if gravatar is enabled (then use gravatar URL)
@ -85,7 +89,8 @@ class UserAvatarHelper
{
//Check if the user has a master attachment defined (meaning he has explicitly defined a profile picture)
if ($user->getMasterPictureAttachment() instanceof Attachment) {
return $this->attachmentURLGenerator->getThumbnailURL($user->getMasterPictureAttachment(), 'thumbnail_sm');
return $this->attachmentURLGenerator->getThumbnailURL($user->getMasterPictureAttachment(), 'thumbnail_sm')
?? throw new RuntimeException('Could not generate thumbnail URL');
}
//If not check if gravatar is enabled (then use gravatar URL)