Created
July 13, 2024 13:55
-
-
Save koriym/3fc42fb3dff7acb3b6fb3cd7828dadf0 to your computer and use it in GitHub Desktop.
Commenting with DeepSeek-Coder-V2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```php | |
<?php declare(strict_types=1); | |
// | |
// Original code: https://github.com/nikic/PHP-Parser/blob/master/lib/PhpParser/JsonDecoder.php | |
// | |
namespace PhpParser; | |
JsonDecoder | |
{ | |
/** @var \ReflectionClass[] Node type to reflection class map */ | |
private $reflectionClassCache; // ノードタイプとリフレクションクラスのマップを保持するプロパティ | |
public function decode(string $json) { | |
$value = json_decode($json, true); // JSON文字列をデコード | |
if (json_last_error()) { | |
throw new \RuntimeException('JSON decoding error: ' . json_last_error_msg()); // デコードエラーがあれば例外を投げる | |
} | |
return $this->decodeRecursive($value); // 再帰的に値をデコード | |
} | |
private function decodeRecursive($value) { | |
if (\is_array($value)) { // 値が配列の場合 | |
if (isset($value['nodeType'])) { // nodeTypeが設定されている場合 | |
if ($value['nodeType'] === 'Comment' || $value['nodeType'] === 'Comment_Doc') { | |
return $this->decodeComment($value); // CommentまたはComment_Docの場合はコメントをデコード | |
} | |
return $this->decodeNode($value); // それ以外のノードをデコード | |
} | |
return $this->decodeArray($value); // nodeTypeが設定されていない配列をデコード | |
} | |
return $value; // 配列でない場合はそのまま返す | |
} | |
private function decodeArray(array $array) : array { | |
$decodedArray = []; // デコードされた配列を初期化 | |
foreach ($array as $key => $value) { | |
$decodedArray[$key] = $this->decodeRecursive($value); // 再帰的に値をデコード | |
} | |
return $decodedArray; // デコードされた配列を返す | |
} | |
private function decodeNode(array $value) : Node { | |
$nodeType = $value['nodeType']; // ノードタイプを取得 | |
if (!\is_string($nodeType)) { | |
throw new \RuntimeException('Node type must be a string'); // ノードタイプが文字列でない場合は例外を投げる | |
} | |
$reflectionClass = $this->reflectionClassFromNodeType($nodeType); // ノイプからリフレクションクラスを取得 | |
/** @var Node $node */ | |
$node = $reflectionClass->newInstanceWithoutConstructor(); // コンストラクタを呼ばずにインスタンスを生成 | |
if (isset($value['attributes'])) { // 属性が設定されている場合 | |
if (!\array($value['attributes'])) { | |
throw new \RuntimeException('Attributes must be an array'); // 属性が配列でない場合は例外を投げる | |
} | |
$node->setAttributes($this->decodeArray($value['attributes'])); // 属性をデコードして設定 | |
} | |
foreach ($value as $name => $subNode) { | |
if ($name === 'nodeType' || $name === 'attributes') { | |
continue; // nodeTypeとattributesはスキップ | |
} | |
$node->$ $this->decodeRecursive($subNode); // 再帰的にサブノードをデコードして設定 | |
} | |
return $node; // デコードされたノードを返す | |
} | |
private function decodeComment(array $value) : Comment { | |
$className = $value['nodeType'] === 'Comment' ? Comment::class : Comment\Doc::class; // コメントタイプに応じてクラス名を設定 | |
if (!isset($value['text'])) { | |
throw new \RuntimeException('Comment must have text'); // テキストが設定されていない場合は例外を投げる | |
} | |
return new $className( | |
$value['text'], | |
$value['line'] ?? -1, $value['filePos'] ?? -1, $value['tokenPos'] ?? -1, | |
$value['endLine'] ?? -1, $value['endFilePos'] ?? -1, $value['endTokenPos'] ?? -1 | |
); // コメントオブジェクトを生成して返す | |
} | |
private function reflectionClassFromNodeType(string $nodeType) : \ReflectionClass { | |
if (!isset($this->reflectionClassCache[$nodeType])) { // リフレクションクラスがキャッシュにない場合 | |
$className = $this->classNameFromNodeType($nodeType); // ノードタイプからクラス名 | |
$this->reflectionClassCache[$nodeType] = new \ReflectionClass($className); // リフレクションクラスを生成してキャッシュ | |
} | |
return $this->reflectionClassCache[$nodeType]; // リフレクションクラスを返す | |
} | |
private function classNameFromNodeType(string $nodeType) : string { | |
$className = 'PhpParser\\Node\\' . strtr($nodeType, '_', '\\'); // ノードタイプからクラス名を生成 | |
if_exists($className)) { | |
return $className; // クラスが存在する場合はそのまま返す | |
} | |
$className .= '_'; // クラス名にアンダースコアを追加 | |
if (class_exists($className)) { | |
return $className; // クラスが存在する場合はそのまま返す | |
} | |
throw new \RuntimeException("Unknown node type \"$nodeType\""); // クラスが存在しない場合は例外を投げる | |
} | |
} | |
// 終わりです | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment