Skip to content

Commit 5929d19

Browse files
committed
docs: enhance code documentation with comprehensive PHPDoc comments
1 parent df53ccc commit 5929d19

File tree

130 files changed

+4838
-542
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+4838
-542
lines changed

.github/FUNDING.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
github: yoeunes
2-
custom: https://www.paypal.com/paypalme/yoeunes

.phpstorm.meta.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
11
<?php
22

3+
/**
4+
* PhpStorm Meta File - IDE Enhancement for PHPFlasher.
5+
*
6+
* This file provides PhpStorm with additional type information and method completion
7+
* capabilities for the PHPFlasher library. It's not loaded during runtime but is used
8+
* exclusively by the IDE to improve developer experience.
9+
*
10+
* The file enhances code intelligence in several ways:
11+
* 1. Adds type inference for Envelope::get() method
12+
* 2. Provides completion for factory methods in FlasherInterface
13+
* 3. Registers expected argument sets for notification types
14+
* 4. Defines expected arguments for various builder methods
15+
*
16+
* Design patterns:
17+
* - Metadata: Provides additional information about code that's only used by tools
18+
* - IDE Integration: Bridges the gap between dynamic PHP code and static analysis tools
19+
*
20+
* Note: This file is part of the development tooling and has no effect on runtime behavior.
21+
*/
22+
323
namespace PHPSTORM_META;
424

25+
// Infer the correct type when Envelope::get() is called with a class name
526
override(Envelope::get(0), type(0));
627

28+
// Map factory methods to their return types for better autocompletion
729
override(\Flasher\Prime\FlasherInterface::create(), map(['flasher' => \Flasher\Prime\Factory\FlasherFactory::class, 'theme.' => \Flasher\Prime\Factory\FlasherFactory::class]));
830
override(\Flasher\Prime\Container\FlasherContainer::create(), map(['flasher' => \Flasher\Prime\Factory\FlasherFactory::class, 'theme.' => \Flasher\Prime\Factory\FlasherFactory::class]));
931
override(\Flasher\Prime\FlasherInterface::use(), map(['flasher' => \Flasher\Prime\Factory\FlasherFactory::class, 'theme.' => \Flasher\Prime\Factory\FlasherFactory::class]));
1032

33+
// Register and utilize notification type constants for better code completion
1134
registerArgumentsSet('types', 'success', 'error', 'warning', 'info');
1235
expectedArguments(\Flasher\Prime\Notification\NotificationBuilderInterface::type(), 0, argumentsSet('types'));
1336
expectedArguments(\Flasher\Prime\Notification\NotificationBuilderInterface::addFlash(), 0, argumentsSet('types'));
@@ -17,7 +40,11 @@
1740
expectedArguments(\Flasher\Prime\flash(), 1, argumentsSet('types'));
1841
expectedReturnValues(\Flasher\Prime\Notification\NotificationInterface::getType(), argumentsSet('types'));
1942

43+
// Define expected arguments for handlers
2044
expectedArguments(\Flasher\Prime\Notification\NotificationBuilderInterface::handler(), 0, 'flasher', 'toastr', 'noty', 'notyf', 'sweetalert');
2145

46+
// Define expected arguments for render formats
2247
expectedArguments(\Flasher\Prime\FlasherInterface::render(), 0, 'html', 'json', 'array');
48+
49+
// Define expected arguments for common option keys
2350
expectedArguments(\Flasher\Prime\Notification\FlasherBuilder::option(), 0, 'timeout', 'timeouts', 'fps', 'position', 'direction', 'rtl', 'style', 'escapeHtml');

Asset/AssetManager.php

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,45 @@
44

55
namespace Flasher\Prime\Asset;
66

7+
/**
8+
* AssetManager - Manages asset files and their versioning via a manifest file.
9+
*
10+
* This class provides functionality for handling asset versioning through a manifest
11+
* file that maps original asset paths to versioned paths with hash parameters.
12+
* The asset versioning helps with cache busting by appending a content hash to the URL.
13+
*
14+
* Design patterns:
15+
* - Resource Manager: Centralized management of web asset resources
16+
* - Cache Buster: Handles versioning of assets to ensure browser cache invalidation
17+
*/
718
final class AssetManager implements AssetManagerInterface
819
{
920
/**
21+
* Cached manifest entries mapping original paths to versioned paths.
22+
*
1023
* @var array<string, string>
1124
*/
1225
private array $entries = [];
1326

14-
public function __construct(private readonly string $publicDir, private readonly string $manifestPath)
15-
{
27+
/**
28+
* Creates a new AssetManager instance.
29+
*
30+
* @param string $publicDir The public directory where assets are located
31+
* @param string $manifestPath The path to the manifest file
32+
*/
33+
public function __construct(
34+
private readonly string $publicDir,
35+
private readonly string $manifestPath,
36+
) {
1637
}
1738

39+
/**
40+
* {@inheritdoc}
41+
*
42+
* This implementation first checks for an exact match in the manifest,
43+
* then tries with directory separators normalized, and falls back to the
44+
* original path if no match is found.
45+
*/
1846
public function getPath(string $path): string
1947
{
2048
$entriesData = $this->getEntriesData();
@@ -27,6 +55,16 @@ public function getPaths(array $paths): array
2755
return array_map(fn (string $path) => $this->getPath($path), $paths);
2856
}
2957

58+
/**
59+
* {@inheritdoc}
60+
*
61+
* This method:
62+
* 1. Processes each file to calculate its content hash
63+
* 2. Creates a mapping from relative paths to versioned paths
64+
* 3. Writes the mapping to the manifest file as JSON
65+
*
66+
* @throws \RuntimeException If unable to write to the manifest file
67+
*/
3068
public function createManifest(array $files): void
3169
{
3270
foreach ($files as $file) {
@@ -51,7 +89,12 @@ public function createManifest(array $files): void
5189
/**
5290
* Loads and returns the entries from the manifest file.
5391
*
54-
* @return array<string, string> the manifest entries
92+
* This method lazily loads the manifest data and caches it for subsequent calls.
93+
* If no manifest file exists or it cannot be parsed, an empty array is returned.
94+
*
95+
* @return array<string, string> The manifest entries mapping original to versioned paths
96+
*
97+
* @throws \InvalidArgumentException If the manifest file contains invalid JSON
5598
*/
5699
private function getEntriesData(): array
57100
{
@@ -73,6 +116,16 @@ private function getEntriesData(): array
73116
return $this->entries = $entries; // @phpstan-ignore-line
74117
}
75118

119+
/**
120+
* Computes a hash for a file based on its contents.
121+
*
122+
* This method normalizes line endings to ensure consistent hashing
123+
* across different platforms.
124+
*
125+
* @param string $path The path to the file
126+
*
127+
* @return string The MD5 hash of the file contents or empty string if file cannot be read
128+
*/
76129
private function computeHash(string $path): string
77130
{
78131
$contents = file_get_contents($path);

Asset/AssetManagerInterface.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,49 @@
44

55
namespace Flasher\Prime\Asset;
66

7+
/**
8+
* AssetManagerInterface - Contract for asset management services.
9+
*
10+
* This interface defines the essential operations for managing web assets,
11+
* particularly focusing on path resolution and manifest generation for versioning.
12+
*
13+
* Design pattern: Strategy - Defines a common interface for different asset
14+
* management strategies that can be used interchangeably.
15+
*/
716
interface AssetManagerInterface
817
{
918
/**
1019
* Resolves the given path to its hashed version if available in the manifest.
1120
*
12-
* @param string $path the original file path
21+
* This method should look up the original path in a manifest of versioned assets
22+
* and return the versioned path if found. Otherwise, it should return the original path.
1323
*
14-
* @return string the resolved file path or the original path if not found in the manifest
24+
* @param string $path The original file path
25+
*
26+
* @return string The resolved file path with version hash or the original path if not found
1527
*/
1628
public function getPath(string $path): string;
1729

1830
/**
19-
* @param string[] $paths
31+
* Resolves multiple paths to their hashed versions if available.
32+
*
33+
* This is a batch version of getPath() that processes multiple paths at once.
34+
*
35+
* @param string[] $paths Array of original file paths
2036
*
21-
* @return string[]
37+
* @return string[] Array of resolved file paths in the same order
2238
*/
2339
public function getPaths(array $paths): array;
2440

2541
/**
26-
* Generates a json manifest from given files.
42+
* Generates a JSON manifest from given files.
43+
*
44+
* This method should create a mapping between original file paths and
45+
* versioned paths (typically with content hashes) and save it to a manifest file.
46+
*
47+
* @param string[] $files Array of file paths to include in the manifest
2748
*
28-
* @param string[] $files array of file paths
49+
* @throws \RuntimeException If the manifest cannot be created
2950
*/
3051
public function createManifest(array $files): void;
3152
}

Configuration.php

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@
55
namespace Flasher\Prime;
66

77
/**
8+
* Configuration for PHPFlasher.
9+
*
10+
* This class provides type-safe access to PHPFlasher's configuration structure.
11+
* It uses PHPStan annotations to define the complex nested configuration schema,
12+
* ensuring configuration consistency and enabling IDE autocompletion.
13+
*
14+
* Design pattern: Type-safe Configuration - Uses PHP's type system to validate
15+
* and document complex configuration structures.
16+
*
17+
* The configuration array structure contains:
18+
* - default: The default notification adapter to use
19+
* - main_script: Main JavaScript file path
20+
* - scripts: Additional JavaScript files to include
21+
* - styles: CSS stylesheets to include
22+
* - inject_assets: Whether to auto-inject assets in responses
23+
* - translate: Whether to translate notifications
24+
* - excluded_paths: Paths where notifications shouldn't be displayed
25+
* - options: Global notification options
26+
* - filter: Default filtering criteria
27+
* - flash_bag: Flash bag configuration for framework integration
28+
* - presets: Notification templates/presets with type, title, message and options
29+
* - plugins: Plugin-specific configuration with scripts, styles and options
30+
*
831
* @phpstan-type ConfigType array{
932
* default: string,
1033
* main_script?: string,
@@ -20,45 +43,26 @@
2043
* type: string,
2144
* title: string,
2245
* message: string,
23-
* options: array<string, mixed>,
46+
* options: array<string, mixed>
2447
* }>,
2548
* plugins?: array<string, array{
2649
* scripts?: string[],
2750
* styles?: string[],
28-
* options?: array<string, mixed>,
29-
* }>,
51+
* options?: array<string, mixed>
52+
* }>
3053
* }
3154
*/
3255
final class Configuration
3356
{
3457
/**
35-
* @param array{
36-
* default: string,
37-
* main_script?: string,
38-
* scripts?: string[],
39-
* styles?: string[],
40-
* inject_assets?: bool,
41-
* translate?: bool,
42-
* excluded_paths?: list<non-empty-string>,
43-
* options?: array<string, mixed>,
44-
* filter?: array<string, mixed>,
45-
* flash_bag?: false|array<string, string[]>,
46-
* presets?: array<string, array{
47-
* type: string,
48-
* title: string,
49-
* message: string,
50-
* options: array<string, mixed>,
51-
* }>,
52-
* plugins?: array<string, array{
53-
* scripts?: string[],
54-
* styles?: string[],
55-
* options?: array<string, mixed>,
56-
* }>,
57-
* } $config
58+
* Validates and returns the configuration array.
59+
*
60+
* This method serves as a type-safe wrapper around configuration arrays,
61+
* ensuring that they match the expected schema defined in the PHPStan annotations.
5862
*
5963
* @phpstan-param ConfigType $config
6064
*
61-
* @return ConfigType
65+
* @phpstan-return ConfigType
6266
*/
6367
public static function from(array $config): array
6468
{

0 commit comments

Comments
 (0)