Skip to content

Commit

Permalink
♻️ Refactored DocBlockWriter class to DocumentationGenerator, added s…
Browse files Browse the repository at this point in the history
…tatic createDocBlock method, and added writeDocBlock method to FileWriter class for writing docblocks to files.
  • Loading branch information
molbal committed Jan 8, 2023
1 parent 1be665b commit 9574ebd
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
34 changes: 34 additions & 0 deletions src/DocumentationGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Molbal\AiPhpdoc;

class DocumentationGenerator
{
public static function createDocBlock(string $function): string {
$key = getenv('OPENAI_KEY');

$openai = \OpenAI::client($key);

$completion = $openai->completions()->create([
'model' => 'text-davinci-003',
'prompt' => 'Read the following PHP function: """ '.$function.' """ PHPDoc block for the function:',
'max_tokens' => 1024,
'stop' => ['"""'],
'temperature' => 0.3
]);

try {
// Check if the OpenAI API returned an error response
if (isset($completion['error'])) {
throw new \RuntimeException($completion['error']);
}

return $completion['choices'][0]['text'];
}
catch (\Throwable $e) {
throw new \RuntimeException('An error occurred while trying to get the doc block: ' . $e->getMessage());
}
}


}
14 changes: 14 additions & 0 deletions src/FileWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Molbal\AiPhpdoc;

class FileWriter
{
public static function writeDocBlock(string $file, string $body, string $docblock): bool
{
return file_put_contents(
$file,
str_replace($body, $docblock.PHP_EOL.$body, file_get_contents($file))
) !== false;
}
}
47 changes: 44 additions & 3 deletions src/InsertMissingDocs.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,64 @@
class InsertMissingDocs extends Command
{


/**
* @return void
* Configures the current command.
*
* @param string $file The file to list the functions of
*/
protected function configure()
{
$this->addArgument('file', InputArgument::REQUIRED, 'The file to list the functions of');
}


/**
* Execute the function
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$filePath = $input->getArgument('file');
try {
$functions = FileParser::getFunctionsFromFile($filePath);
$errors = 0;
$completions = 0;
foreach ($functions as $function) {
$output->writeln($function['name'] . ': ' . ($function['phpdoc'] ? 'yes' : 'no'));
if (!$function['phpdoc']) {
$output->writeln('Found function without docblock: '.$function['name'].'');
try {
$docs = DocumentationGenerator::createDocBlock($function['body']);
if (FileWriter::writeDocBlock($filePath, $function['body'], $docs)) {
$output->writeln('<info>Wrote docblock for '.$function['name'].'</info>');
$completions++;
}
else {
if (FileWriter::writeDocBlock($filePath, $function['body'], $docs)) {
$output->writeln('<error>Generated docblock for '.$function['name'].', but could not write it to the file.</error>');
$output->writeln($docs);
$errors++;
}
}
}
catch (\Exception $error) {
$output->writeln('<error>Could not generate docblock for '.$function['name'].': '.$error->getMessage().'</error>');
}
}
}
return Command::SUCCESS;

if (empty($functions)) {
$output->writeln('<comment>🙈 No functions found in the file.</comment>');
}

if ($completions > 0) {
$output->writeln('Finished processing '.$filePath.' with '.$completions.' docblocks written and '.$errors.' errors.');
}

return $errors > 0 ? Command::FAILURE : Command::SUCCESS;
} catch (\Exception $e) {
$output->writeln('Error: ' . $e->getMessage());
return Command::FAILURE;
Expand Down

0 comments on commit 9574ebd

Please sign in to comment.