rector fixes + bug fixes

This commit is contained in:
Dennis Eichhorn 2023-05-27 03:06:45 +00:00
parent 185d5fcf1e
commit 9ee8684d57
5 changed files with 113 additions and 271 deletions

42
Config/rector.php Normal file
View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector;
use Rector\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector;
use Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector;
use Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector;
use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector;
use Rector\Config\RectorConfig;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
use Rector\EarlyReturn\Rector\Return_\ReturnBinaryAndToEarlyReturnRector;
use Rector\Set\ValueObject\SetList;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/Model',
__DIR__ . '/Modules',
__DIR__ . '/phpOMS',
]);
// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
$rectorConfig->sets([
SetList::CODE_QUALITY,
]);
$rectorConfig->skip([
SimplifyEmptyCheckOnEmptyArrayRector::class,
FlipTypeControlToUseExclusiveTypeRector::class,
UnusedForeachValueToArrayKeysRector::class,
ReturnBinaryAndToEarlyReturnRector::class,
SimplifyRegexPatternRector::class,
RemoveAlwaysElseRector::class,
OptionalParametersAfterRequiredRector::class,
RemoveExtraParametersRector::class,
CompleteDynamicPropertiesRector::class,
]);
};

View File

@ -13,7 +13,7 @@ add-apt-repository ppa:ondrej/php
apt-get update
apt-get install npm git composer cmake php8.1 php8.1-dev php8.1-cli php8.1-common php8.1-mysql php8.1-pgsql php8.1-xdebug php8.1-opcache php8.1-pdo php8.1-sqlite php8.1-mbstring php8.1-curl php8.1-imap php8.1-bcmath php8.1-zip php8.1-dom php8.1-xml php8.1-phar php8.1-gd php-pear apache2 mysql-server postgresql postgresql-contrib pcov
apt-get install npm git composer cmake php8.1 php8.1-dev php8.1-cli php8.1-common php8.1-mysql php8.1-pgsql php8.1-xdebug php8.1-opcache php8.1-pdo php8.1-sqlite php8.1-mbstring php8.1-curl php8.1-imap php8.1-bcmath php8.1-zip php8.1-dom php8.1-xml php8.1-phar php8.1-gd php-pear apache2 mysql-server postgresql postgresql-contrib pcov xvfb cutycapt
# USE mysql;
# mysql < 5.7

View File

@ -9,7 +9,7 @@ use phpOMS\Uri\HttpUri;
class GptHelper
{
private const ENDPOINT = 'https://api.openai.com/v1/chat/completions';
private const APIKEY = '';
private const APIKEY = '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';
private const MODEL = 'gpt-3.5-turbo';
private const TEMPERATUR = 0.9;
private const MAX_TOKENS = 1700;
@ -66,7 +66,7 @@ class GptHelper
|| \stripos($response->getBody(), '503 Service') !== false
);
return \str_replace('```', '', $response->getBody());
return $response->getBody();
}
public static function handleFile(string $inPath, string $outPath, string $behavior, \Closure $fileReader, bool $bulk = true) : string
@ -94,6 +94,10 @@ class GptHelper
continue;
}
if(\stripos($outPath, '.php') !== false) {
$response = \str_replace('```', '', $response);
}
$json = \json_decode($response, true);
\fwrite($out, $json[5][0]['message']['content']);
\fwrite($out, "\n");
@ -110,6 +114,10 @@ class GptHelper
$response = self::aiRequest($behavior, $lines);
if ($response !== '' && $response !== false) {
if(\stripos($outPath, '.php') !== false) {
$response = \str_replace('```', '', $response);
}
$json = \json_decode($response, true);
\fwrite($out, $json[5][0]['message']['content']);
\fwrite($out, "\n");

View File

@ -0,0 +1,60 @@
<?php
use Build\Tools\AutoGpt\GptHelper;
include __DIR__ . '/../../../phpOMS/Autoloader.php';
// fix docblocks
$globs = [
__DIR__ . '/../../../phpOMS/**/*.php',
__DIR__ . '/../../../Modules/*/Models/*.php',
__DIR__ . '/../../../Modules/*/Controller/*.php',
];
$behaviour = <<<BEHAVIOUR
You are a developer, create a developer documentation for the following code using markdown.
BEHAVIOUR;
$fileReader = function ($in, $filename)
{
if (\filesize($filename) > 1300) {
return false;
}
$lines = '';
$isFirstLine = true;
while (($line = \fgets($in)) !== false) {
$isFirstLine = false;
$lines .= $line;
}
if ($isFirstLine && empty($lines)) {
return false;
}
return $lines;
};
$output = __DIR__ . '/../../../doc.md';
$md = \fopen($output, 'w');
foreach ($globs as $glob) {
$files = \glob($glob);
foreach ($files as $file) {
if (\stripos($file, 'Test.php')) {
continue;
}
$response = GptHelper::handleFile($file, 'php://memory', $behaviour, $fileReader);
if (\trim($response) !== '') {
\fwrite($md, $file . ":\n");
\fwrite($md, $response);
\fwrite($md, "\n\n");
}
}
}
\fclose($md);

View File

@ -1,268 +0,0 @@
<?php
use Build\Helper\GptHelper;
include __DIR__ . '/../../../phpOMS/Autoloader.php';
// fix docblocks
$globs = [
__DIR__ . '/../../../phpOMS/**/*.php',
__DIR__ . '/../../../Modules/*/Models/*.php',
__DIR__ . '/../../../Modules/*/Controller/*.php',
];
$behaviour = <<<BEHAVIOUR
Create missing php docblocks with a similar format for member variables:
/**
* Default delivery address.
*
* @var Address|null
* @since 1.0.0
*/
and for functions:
/**
* Order contact elements
*
* @param ContactElement \$ele Element
* @param bool \$isRequired Is required
*
* @return int
*
* @since 1.0.0
*/
Only output the php code, nothing else. Also don't add or change any new php code except the docblocks.
BEHAVIOUR;
$fileReader = function ($in, $filename)
{
$lines = '';
$scopeStart = false;
$scopeCounter = 0;
$isFirstLine = true;
while (($line = \fgets($in)) !== false && (\trim($line) !== '' || $scopeCounter > 0)) {
$isFirstLine = false;
if (\stripos($line, ' function ') !== false) {
$scopeStart = true;
}
if ($scopeStart) {
$scopeCounter += \substr_count($line, '{');
$scopeCounter -= \substr_count($line, '}');
if ($scopeCounter === 0) {
$scopeStart = false;
}
}
$lines .= $line;
}
if ($isFirstLine && empty($lines)) {
return false;
}
return $lines;
};
foreach ($globs as $glob) {
$files = \glob($glob);
foreach ($files as $file) {
if (\stripos($file, 'Test.php')) {
continue;
}
GptHelper::handleFile($file, $file, $behaviour, $fileReader);
}
}
// implement test
$globs = [
__DIR__ . '/../../phpOMS/**.php',
__DIR__ . '/../../Modules/*/Models/*.php',
];
$behaviour = <<<BEHAVIOUR
Implement a php unit test for the given function.
BEHAVIOUR;
$fileReader = function ($in, $filename)
{
$lines = '';
$scopeStart = false;
$scopeCounter = 0;
$isFirstLine = true;
while (($line = \fgets($in)) !== false && ($line !== '' || $scopeCounter > 0)) {
$isFirstLine = false;
if (\stripos($line, 'public function ') !== false || \stripos($line, 'public static function ') !== false) {
// Check if a test exists for this function
$testFile = \str_replace('.php', 'Test.php', $filename);
$testFile = \str_replace('/phpOMS/', '/phpOMS/tests/', $filename);
if (\stripos($testFile, '../Modules/') !== false) {
$subdirectory = "tests/";
$position = strrpos($testFile, "/../");
if ($position !== false) {
$position = \stripos($testFile, '/', $position + 4) + 1;
$position = \stripos($testFile, '/', $position) + 1;
$testFile = \substr_replace($testFile, $subdirectory, $position, 0);
}
}
if (\file_exists($testFile)) {
$scopeStart = true;
$testContent = \file_get_contents($testFile);
// Parse functio name
$pattern = '/\bfunction\b\s+(\w+)\s*\(/';
\preg_match($pattern, $line, $matches);
if (\stripos($testContent, $matches[1] ?? '~~~') !== false) {
// Test already exists
$scopeStart = false;
}
}
$scopeStart = true;
}
// Not in a function / not relevant
if (!$scopeStart) {
continue;
}
if ($scopeStart) {
$scopeCounter += \substr_count($line, '{');
$scopeCounter -= \substr_count($line, '}');
if ($scopeCounter === 0) {
$scopeStart = false;
}
}
$lines .= $line;
}
if ($isFirstLine && empty($lines)) {
return false;
}
return $lines;
};
foreach ($globs as $glob) {
$files = \glob($glob);
foreach ($files as $file) {
if (\stripos($file, 'Test.php')) {
continue;
}
$response = GptHelper::handleFile($file, 'php://memory', $behaviour, $fileReader, false);
echo $response , \PHP_EOL , \PHP_EOL;
}
}
// complete and improve test
$globs = [
__DIR__ . '/../../phpOMS/**.php',
__DIR__ . '/../../Modules/*/Models/*.php',
];
$behaviour = <<<BEHAVIOUR
Improve the given php unit test for the given function.
BEHAVIOUR;
$fileReader = function ($in, $filename)
{
$lines = '';
$scopeStart = false;
$scopeCounter = 0;
$isFirstLine = true;
while (($line = \fgets($in)) !== false && ($line !== '' || $scopeCounter > 0)) {
$isFirstLine = false;
if (\stripos($line, 'public function ') !== false || \stripos($line, 'public static function ') !== false) {
// Check if a test exists for this function
$testFile = \str_replace('.php', 'Test.php', $filename);
$testFile = \str_replace('/phpOMS/', '/phpOMS/tests/', $filename);
if (\stripos($testFile, '../Modules/') !== false) {
$subdirectory = "tests/";
$position = strrpos($testFile, "/../");
if ($position !== false) {
$position = \stripos($testFile, '/', $position + 4) + 1;
$position = \stripos($testFile, '/', $position) + 1;
$testFile = \substr_replace($testFile, $subdirectory, $position, 0);
}
}
if (\file_exists($testFile)) {
$scopeStart = true;
$testContent = \file_get_contents($testFile);
// Parse functio name
$pattern = '/\bfunction\b\s+(\w+)\s*\(/';
\preg_match($pattern, $line, $matches);
if (\stripos($testContent, $matches[1] ?? '~~~') !== false) {
// Test already exists
$scopeStart = false;
}
$scopeStart = true;
} else {
$scopeStart = false;
}
}
// Not in a function / not relevant
if (!$scopeStart) {
continue;
}
if ($scopeStart) {
$scopeCounter += \substr_count($line, '{');
$scopeCounter -= \substr_count($line, '}');
if ($scopeCounter === 0) {
$scopeStart = false;
}
}
$lines .= $line;
}
if ($isFirstLine && empty($lines)) {
return false;
}
return $lines;
};
foreach ($globs as $glob) {
$files = \glob($glob);
foreach ($files as $file) {
if (\stripos($file, 'Test.php')) {
continue;
}
$response = GptHelper::handleFile($file, 'php://memory', $behaviour, $fileReader);
echo $response , \PHP_EOL , \PHP_EOL;
}
}