Skip to content

Commit b74704c

Browse files
committed
Add PHPDoc
1 parent 4d815ad commit b74704c

14 files changed

Lines changed: 431 additions & 96 deletions

src/Command/AbstractCommand.php

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,24 @@
3030

3131
use function Safe\getcwd;
3232

33+
/**
34+
* Provides a base configuration and common utilities for Composer commands.
35+
* Extending classes MUST rely on this base abstraction to interact with the console
36+
* application gracefully, and SHALL adhere to the expected return types for commands.
37+
*/
3338
abstract class AbstractCommand extends BaseCommand
3439
{
40+
/**
41+
* @var Filesystem The filesystem instance used for file operations. This property MUST be utilized for interacting with the file system securely.
42+
*/
3543
protected readonly Filesystem $filesystem;
3644

3745
/**
38-
* @param Filesystem|null $filesystem
46+
* Constructs a new AbstractCommand instance.
47+
*
48+
* The method MAY accept a Filesystem instance; if omitted, it SHALL instantiate a new one.
49+
*
50+
* @param Filesystem|null $filesystem the filesystem utility to use
3951
*/
4052
public function __construct(?Filesystem $filesystem = null)
4153
{
@@ -45,10 +57,16 @@ public function __construct(?Filesystem $filesystem = null)
4557
}
4658

4759
/**
48-
* @param Process $command
49-
* @param OutputInterface $output
60+
* Executes a given system process gracefully and outputs its buffer.
61+
*
62+
* The method MUST execute the provided command ensuring the output is channeled
63+
* to the OutputInterface. It SHOULD leverage TTY if supported. If the process
64+
* fails, it MUST return `self::FAILURE`; otherwise, it SHALL return `self::SUCCESS`.
65+
*
66+
* @param Process $command the configured process instance to run
67+
* @param OutputInterface $output the output interface to log warnings or results
5068
*
51-
* @return int
69+
* @return int the status code of the command execution
5270
*/
5371
protected function runProcess(Process $command, OutputInterface $output): int
5472
{
@@ -86,7 +104,12 @@ protected function runProcess(Process $command, OutputInterface $output): int
86104
}
87105

88106
/**
89-
* @return string
107+
* Retrieves the current working directory of the application.
108+
*
109+
* The method MUST return the initial working directory defined by the application.
110+
* If not available, it SHALL fall back to the safe current working directory.
111+
*
112+
* @return string the absolute path to the current working directory
90113
*/
91114
protected function getCurrentWorkingDirectory(): string
92115
{
@@ -95,9 +118,14 @@ protected function getCurrentWorkingDirectory(): string
95118
}
96119

97120
/**
98-
* @param string $relativePath
121+
* Computes the absolute path for a given relative or absolute path.
99122
*
100-
* @return string
123+
* This method MUST return the exact path if it is already absolute.
124+
* If relative, it SHALL make it absolute relying on the current working directory.
125+
*
126+
* @param string $relativePath the path to evaluate or resolve
127+
*
128+
* @return string the resolved absolute path
101129
*/
102130
protected function getAbsolutePath(string $relativePath): string
103131
{
@@ -109,10 +137,15 @@ protected function getAbsolutePath(string $relativePath): string
109137
}
110138

111139
/**
112-
* @param string $filename
113-
* @param bool $force
140+
* Determines the correct absolute path to a configuration file.
141+
*
142+
* The method MUST attempt to resolve the configuration file locally in the working directory.
143+
* If absent and not forced, it SHALL provide the default equivalent from the package itself.
114144
*
115-
* @return string
145+
* @param string $filename the name of the configuration file
146+
* @param bool $force determines whether to bypass fallback and forcefully return the local file path
147+
*
148+
* @return string the resolved absolute path to the configuration file
116149
*/
117150
protected function getConfigFile(string $filename, bool $force = false): string
118151
{
@@ -128,11 +161,16 @@ protected function getConfigFile(string $filename, bool $force = false): string
128161
}
129162

130163
/**
131-
* @param InputInterface $input
132-
* @param string $commandName
133-
* @param OutputInterface $output
164+
* Configures and executes a registered console command by name.
165+
*
166+
* The method MUST look up the command from the application and run it. It SHALL ignore generic
167+
* validation errors and route the custom input and output correctly.
134168
*
135-
* @return int
169+
* @param string $commandName the name of the required command
170+
* @param array|InputInterface $input the input arguments or array definition
171+
* @param OutputInterface $output the interface for buffering output
172+
*
173+
* @return int the status code resulting from the dispatched command
136174
*/
137175
protected function runCommand(string $commandName, array|InputInterface $input, OutputInterface $output): int
138176
{
@@ -149,7 +187,12 @@ protected function runCommand(string $commandName, array|InputInterface $input,
149187
}
150188

151189
/**
152-
* @return array
190+
* Retrieves configured PSR-4 namespaces from the composer configuration.
191+
*
192+
* This method SHALL parse the underlying `composer.json` using the Composer instance,
193+
* and MUST provide an empty array if no specific paths exist.
194+
*
195+
* @return array the PSR-4 namespaces mappings
153196
*/
154197
protected function getPsr4Namespaces(): array
155198
{
@@ -161,7 +204,12 @@ protected function getPsr4Namespaces(): array
161204
}
162205

163206
/**
164-
* @return string
207+
* Computes the human-readable title or description of the current application.
208+
*
209+
* The method SHOULD utilize the package description as the title, but MUST provide
210+
* the raw package name as a fallback mechanism.
211+
*
212+
* @return string the computed title or description string
165213
*/
166214
protected function getTitle(): string
167215
{

src/Command/CodeStyleCommand.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,23 @@
2323
use Symfony\Component\Console\Output\OutputInterface;
2424
use Symfony\Component\Process\Process;
2525

26+
/**
27+
* Represents the command responsible for checking and fixing code style issues.
28+
* This class MUST NOT be overridden and SHALL rely on external tools like ECS and Composer Normalize.
29+
*/
2630
final class CodeStyleCommand extends AbstractCommand
2731
{
32+
/**
33+
* @var string the default configuration file used for EasyCodingStandard
34+
*/
2835
public const string CONFIG = 'ecs.php';
2936

3037
/**
38+
* Configures the current command.
39+
*
40+
* This method MUST define the name, description, help text, and options for the command.
41+
* It SHALL register the `--fix` option to allow automatic resolutions of style issues.
42+
*
3143
* @return void
3244
*/
3345
protected function configure(): void
@@ -45,10 +57,15 @@ protected function configure(): void
4557
}
4658

4759
/**
48-
* @param InputInterface $input
49-
* @param OutputInterface $output
60+
* Executes the code style checks and fixes block.
61+
*
62+
* The method MUST execute `composer update --lock`, `composer normalize`, and ECS using secure processes.
63+
* It SHALL return `self::SUCCESS` if all commands succeed, or `self::FAILURE` otherwise.
64+
*
65+
* @param InputInterface $input the input interface to retrieve options
66+
* @param OutputInterface $output the output interface to log messages
5067
*
51-
* @return int
68+
* @return int the status code of the command
5269
*/
5370
protected function execute(InputInterface $input, OutputInterface $output): int
5471
{

src/Command/DocsCommand.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,18 @@
2323
use Symfony\Component\Console\Output\OutputInterface;
2424
use Symfony\Component\Process\Process;
2525

26+
/**
27+
* Handles the generation of API documentation for the project.
28+
* This class MUST NOT be extended and SHALL utilize phpDocumentor to accomplish its task.
29+
*/
2630
final class DocsCommand extends AbstractCommand
2731
{
2832
/**
33+
* Configures the command instance.
34+
*
35+
* The method MUST set up the name and description. It MAY accept an optional `--target` option
36+
* pointing to an alternative configuration target path.
37+
*
2938
* @return void
3039
*/
3140
protected function configure(): void
@@ -43,10 +52,15 @@ protected function configure(): void
4352
}
4453

4554
/**
46-
* @param InputInterface $input
47-
* @param OutputInterface $output
55+
* Executes the generation of the documentation files.
56+
*
57+
* This method MUST compile arguments based on PSR-4 namespaces to feed into phpDocumentor.
58+
* It SHOULD provide feedback on generation progress, and SHALL return `self::SUCCESS` on success.
59+
*
60+
* @param InputInterface $input the input details for the command
61+
* @param OutputInterface $output the output mechanism for logging
4862
*
49-
* @return int
63+
* @return int the final execution status code
5064
*/
5165
protected function execute(InputInterface $input, OutputInterface $output): int
5266
{
@@ -56,6 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5670
$this->getAbsolutePath('vendor/bin/phpdoc'),
5771
'--cache-folder',
5872
$this->getCurrentWorkingDirectory() . '/tmp/cache/phpdoc',
73+
'--visibility=public,protected',
5974
];
6075

6176
$psr4Namespaces = $this->getPsr4Namespaces();
@@ -81,6 +96,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8196
...$arguments,
8297
'--target',
8398
$this->getCurrentWorkingDirectory() . '/docs/wiki',
99+
'--visibility=public,protected',
84100
'--template',
85101
'vendor/saggre/phpdocumentor-markdown/themes/markdown',
86102
]);

src/Command/PhpDocCommand.php

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,27 @@
2727

2828
use function Safe\file_get_contents;
2929

30+
/**
31+
* Provides operations to inspect, lint, and repair PHPDoc comments across the project.
32+
* The class MUST NOT be extended and SHALL coordinate tools like PHP-CS-Fixer and Rector.
33+
*/
3034
final class PhpDocCommand extends AbstractCommand
3135
{
36+
/**
37+
* @var string determines the template filename for docheaders
38+
*/
3239
public const string FILENAME = '.docheader';
3340

41+
/**
42+
* @var string stores the underlying configuration file for PHP-CS-Fixer
43+
*/
3444
public const string CONFIG = '.php-cs-fixer.dist.php';
3545

3646
/**
47+
* Configures the PHPDoc command.
48+
*
49+
* This method MUST securely configure the expected inputs, such as the `--fix` option.
50+
*
3751
* @return void
3852
*/
3953
protected function configure(): void
@@ -51,10 +65,15 @@ protected function configure(): void
5165
}
5266

5367
/**
54-
* @param InputInterface $input
55-
* @param OutputInterface $output
68+
* Executes the PHPDoc checks and rectifications.
5669
*
57-
* @return int
70+
* The method MUST ensure the `.docheader` template is present. It SHALL then invoke
71+
* PHP-CS-Fixer and Rector. If both succeed, it MUST return `self::SUCCESS`.
72+
*
73+
* @param InputInterface $input the command input parameters
74+
* @param OutputInterface $output the system output handler
75+
*
76+
* @return int the success or failure state
5877
*/
5978
protected function execute(InputInterface $input, OutputInterface $output): int
6079
{
@@ -69,10 +88,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6988
}
7089

7190
/**
72-
* @param InputInterface $input
73-
* @param OutputInterface $output
91+
* Executes the PHP-CS-Fixer checks internally.
92+
*
93+
* The method SHOULD run in dry-run mode unless the fix flag is explicitly provided.
94+
* It MUST return an integer describing the exit code.
95+
*
96+
* @param InputInterface $input the parsed console inputs
97+
* @param OutputInterface $output the configured outputs
7498
*
75-
* @return mixed
99+
* @return int the status result of the underlying process
76100
*/
77101
private function runPhpCsFixer(InputInterface $input, OutputInterface $output): int
78102
{
@@ -94,10 +118,15 @@ private function runPhpCsFixer(InputInterface $input, OutputInterface $output):
94118
}
95119

96120
/**
97-
* @param InputInterface $input
98-
* @param OutputInterface $output
121+
* Runs Rector to insert missing method block comments automatically.
99122
*
100-
* @return mixed
123+
* The method MUST apply the `AddMissingMethodPhpDocRector` constraint locally.
124+
* It SHALL strictly return an integer denoting success or failure.
125+
*
126+
* @param InputInterface $input the incoming console parameters
127+
* @param OutputInterface $output the outgoing console display
128+
*
129+
* @return int the code indicating the process result
101130
*/
102131
private function runRector(InputInterface $input, OutputInterface $output): int
103132
{
@@ -122,7 +151,12 @@ private function runRector(InputInterface $input, OutputInterface $output): int
122151
}
123152

124153
/**
125-
* @param OutputInterface $output
154+
* Creates the missing document header configuration file if needed.
155+
*
156+
* The method MUST query the local filesystem. If the file is missing, it SHOULD copy
157+
* the tool template into the root folder.
158+
*
159+
* @param OutputInterface $output the logger where missing capabilities are announced
126160
*
127161
* @return void
128162
*/

src/Command/RefactorCommand.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,23 @@
2323
use Symfony\Component\Console\Output\OutputInterface;
2424
use Symfony\Component\Process\Process;
2525

26+
/**
27+
* Provides functionality to execute automated code refactoring using Rector.
28+
* This class MUST NOT be extended and SHALL encapsulate the logic for Rector invocation.
29+
*/
2630
final class RefactorCommand extends AbstractCommand
2731
{
32+
/**
33+
* @var string the default Rector configuration file
34+
*/
2835
public const string CONFIG = 'rector.php';
2936

3037
/**
38+
* Configures the refactor command options and description.
39+
*
40+
* This method MUST define the expected `--fix` option. It SHALL configure the command name
41+
* and descriptions accurately.
42+
*
3143
* @return void
3244
*/
3345
protected function configure(): void
@@ -45,10 +57,15 @@ protected function configure(): void
4557
}
4658

4759
/**
48-
* @param InputInterface $input
49-
* @param OutputInterface $output
60+
* Executes the refactoring process securely.
61+
*
62+
* The method MUST execute Rector securely via `Process`. It SHALL use dry-run mode
63+
* unless the `--fix` option is specified. It MUST return `self::SUCCESS` or `self::FAILURE`.
64+
*
65+
* @param InputInterface $input the input interface to retrieve arguments properly
66+
* @param OutputInterface $output the output interface to log outputs
5067
*
51-
* @return int
68+
* @return int the status code denoting success or failure
5269
*/
5370
protected function execute(InputInterface $input, OutputInterface $output): int
5471
{

0 commit comments

Comments
 (0)