レビューは見つかりませんでした
「動かないコード」「遅い処理」「テストがない」を根本解決。
目的・仕様・制約を入力するだけで、実行可能コード+テストコード+最適化提案+実行手順+品質チェックを自動生成。
Python / Node.js / Goなど主要言語に対応し、設計→実装→検証→最適化をワンストップで完結できます。
要件定義から実行可能ソースコードを生成
単体テスト(pytest / Jest など)を同時出力
tree形式のファイル構成を提示
パフォーマンス最適化提案を自動生成(メモリ・速度・構造)
品質チェックリスト(10項目)で安全性を担保
将来拡張案(設計・依存構造の改善)も併記
副業・個人開発者:スピード納品・PoC構築を自動化したい方
チーム開発者:コード品質の平準化を図りたい方
教育・レビュー担当者:正しい設計と改善パターンを共有したい方
言語:Python / Node.js(TypeScript) / Go
実行環境:Linux / Windows / Docker
テスト:pytest / Jest / Go test
コードスタイル:PEP8 / ESLint(airbnb) / gofmt
入力テンプレに言語・目的・入出力・制約を記入
完成形プロンプトを投入し、コード&テストを生成
提示された手順・コマンドで即実行→最適化提案を反映
以下は、Laravel 8 環境で「CSVを読み込み → 重複排除 → 集計 → JSON出力」を実現するための完全な設計・実装一式です。
標準ライブラリのみを使用し、Amazon Linux 2 でそのまま動作する形にしています。
ファイル構成(tree形式)
実装コード(app/Console/Commands/ProcessCsvCommand.php)
単体テスト(tests/Feature/ProcessCsvCommandTest.php)
実行手順
パフォーマンス最適化(想定ボトルネックと対策)
品質チェックリスト(10項目)
将来拡張案(3件)
laravel-app/ ├── app/ │ └── Console/ │ └── Commands/ │ └── ProcessCsvCommand.php ├── tests/ │ └── Feature/ │ └── ProcessCsvCommandTest.php ├── storage/ │ └── app/ │ └── data.csv # 入力ファイル │ └── result.json # 出力ファイル
app/Console/Commands/ProcessCsvCommand.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
/**
* CSVファイルを読み込み、重複排除・集計してJSONに出力するコマンド。
*
* - 入力: storage/app/data.csv (列: id,name,score)
* - 出力: storage/app/result.json
*
* 制約: 標準ライブラリのみ使用
*/
class ProcessCsvCommand extends Command
{
protected $signature = 'csv:process';
protected $description = 'Read CSV, remove duplicates, aggregate and output JSON.';
public function handle(): int
{
$inputPath = storage_path('app/data.csv');
$outputPath = storage_path('app/result.json');
if (!file_exists($inputPath)) {
$this--->error("Input file not found: {$inputPath}");
return 1;
}
$handle = fopen($inputPath, 'r');
if (!$handle) {
$this->error("Failed to open file: {$inputPath}");
return 1;
}
$header = fgetcsv($handle);
if ($header === false || count($header) < 3) {
$this->error("Invalid CSV header.");
return 1;
}
$data = [];
while (($row = fgetcsv($handle)) !== false) {
if (count($row) < 3) {
continue;
}
[$id, $name, $score] = $row;
$id = trim($id);
$name = trim($name);
$score = (float) $score;
// 重複排除: idが同一なら後勝ち
$data[$id] = ['id' => $id, 'name' => $name, 'score' => $score];
}
fclose($handle);
// 集計(nameごとのscore平均値)
$aggregated = [];
foreach ($data as $record) {
$n = $record['name'];
if (!isset($aggregated[$n])) {
$aggregated[$n] = ['count' => 0, 'sum' => 0];
}
$aggregated[$n]['count']++;
$aggregated[$n]['sum'] += $record['score'];
}
$result = [];
foreach ($aggregated as $name => $stats) {
$result[] = [
'name' => $name,
'average_score' => round($stats['sum'] / $stats['count'], 2),
];
}
file_put_contents($outputPath, json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
$this->info("Processing complete. Output written to: {$outputPath}");
return 0;
}
}
tests/Feature/ProcessCsvCommandTest.php
<?php
namespace Tests\Feature;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class ProcessCsvCommandTest extends TestCase
{
/** @test */
public function it_processes_csv_and_outputs_json()
{
// Arrange: テスト用CSV作成
$csvData = <<<CSV
id,name,score
1,alice,80
2,bob,90
1,alice,85
3,bob,70
CSV;
Storage::disk('local')--->put('data.csv', $csvData);
// Act: コマンド実行
$this->artisan('csv:process')->assertExitCode(0);
// Assert: 出力ファイル存在確認
$this->assertTrue(Storage::disk('local')->exists('result.json'));
$result = json_decode(Storage::disk('local')->get('result.json'), true);
// 検証
$this->assertEquals([
['name' => 'alice', 'average_score' => 85.0],
['name' => 'bob', 'average_score' => 80.0],
], $result);
}
}
1. 環境セットアップ
# Laravel 8 プロジェクト上で composer install php artisan config:cache
2. テスト実行
php artisan test --filter=ProcessCsvCommandTest
3. コマンド実行
php artisan csv:process
4. 入出力例
入力: storage/app/data.csv
id,name,score 1,alice,80 2,bob,90 1,alice,85 3,bob,70
出力: storage/app/result.json
[
{"name": "alice", "average_score": 85.0},
{"name": "bob", "average_score": 80.0}
]
| 想定ボトルネック | 改善案 |
|---|---|
| 大量行読み込み時のメモリ使用 | fgetcsv による逐次処理で既に軽量。極端に大きい場合は yield でジェネレータ化。 |
| JSONエンコード時の一括構築 | ストリーム書き込み方式に変更可能(大規模データ向け)。 |
| ディスクI/O | Amazon Linux で tmpfs を使用してI/OをRAM化可能。 |
簡易ベンチ計測:
time php artisan csv:process
プロファイル確認(Xdebugなしで軽量に):
php -d xdebug.profiler_enable=1 artisan csv:process
| No | 項目 | 状態 |
|---|---|---|
| 1 | 例外処理(ファイル存在・形式検証) | ✅ |
| 2 | ログ/CLI出力 | ✅ |
| 3 | 入力検証(列数チェック) | ✅ |
| 4 | 境界値(空行・空ファイル) | ✅ |
| 5 | I18N(日本語文字含むJSON出力) | ✅ |
| 6 | 日付/時刻依存 | N/A |
| 7 | 並行実行時競合 | N/A(シングルジョブ) |
| 8 | 拡張性(別集計項目) | ✅ |
| 9 | メモリ効率 | ✅ |
| 10 | テスト自動化 | ✅ |
集計関数の拡張
平均値以外に中央値・最大値などをStrategyパターンで差し替え可能に。
ファイル入力の汎用化
引数でファイルパスを指定できるようにして、バッチ処理対応。
Web API化
ArtisanコマンドロジックをServiceクラス化し、REST APIエンドポイント経由でも実行可能に。
以上。
この構成をコピーして php artisan csv:process を実行すれば即動作します。
レビューは見つかりませんでした