Skip to content

Instantly share code, notes, and snippets.

@Leko
Last active March 31, 2016 04:25
Show Gist options
  • Save Leko/56d0fa49bd52f564f5ed839bb3d4d823 to your computer and use it in GitHub Desktop.
Save Leko/56d0fa49bd52f564f5ed839bb3d4d823 to your computer and use it in GitHub Desktop.
PHPのドキュメントを開くツール
#!/usr/bin/env php
<?php
function help()
{
echo <<<HELP
Usage of phpdoc
Command:
phpdoc [name] [lang]
name: explode, SplFileObject::__construct, etc...
lang: ja, en, etc...
Example:
$ phpdoc explode # function
$ phpdoc SplFileObject # class
$ phpdoc Traversable # interface
$ phpdoc PHP_EOL # constant
$ phpdoc PDO::FETCH_ASSOC # object constant
$ phpdoc PDO::quote # object method
$ phpdoc MCRYPT_MODE_ECB # extension constant
HELP;
exit;
}
if (!isset($argv[1])) {
help();
}
$name = $argv[1];
$id = '';
$lang = isset($argv[2]) ? $argv[2] : 'ja';
$functions = get_defined_functions()['internal'];
$interfaces = get_declared_interfaces();
$constants = array_keys(get_defined_constants(true)['Core']);
$classes = get_declared_classes();
// 組み込み関数
if (in_array($name, $functions)) {
$type = 'function';
// 予約済みクラス
} elseif (in_array($name, $classes)) {
$type = 'class';
// 予約済みインタフェース
} elseif (in_array($name, $interfaces)) {
$type = 'class';
// 予約済み定数
} elseif (in_array($name, $constants)) {
$id = '#constant.'.str_replace('_', '-', strtolower($name));
$type = 'reserved';
$name = 'constants';
// オブジェクト定数 or メソッド
} elseif (strpos($name, '::') !== false) {
list($class, $name) = explode('::', $name);
$ref = new ReflectionClass($class);
// メソッド
if ($ref->hasMethod($name)) {
$type = strtolower($class);
$name = strtolower($name);
// 定数
} elseif ($ref->hasConstant($name)) {
$type = strtolower($class);
$id = "#{$type}.constants.".str_replace('_', '-', strtolower($name));
$name = 'constants';
}
} else {
$categorized_constants = get_defined_constants(true);
foreach ($categorized_constants as $ext => $constants) {
if (!isset($constants[$name])) continue;
$type = strtolower($ext);
$name = 'constants';
}
if (!isset($type)) {
throw new Exception("Not found:{$name}");
}
}
$name = str_replace('_', '-', $name);
$name = strtolower($name);
system("open http://php.net/manual/{$lang}/{$type}.{$name}.php{$id}");

Note: This command works OSX only.

Install

curl -sS https://gist.githubusercontent.com/Leko/56d0fa49bd52f564f5ed839bb3d4d823/raw/f6405d38b20d5bc21feb35f8be473a5f864dd0f0/phpdoc > /usr/local/bin/phpdoc
chmod +x /usr/local/bin/phpdoc

phpdoc

Usage

phpdoc explode          # function
phpdoc SplFileObject    # class
phpdoc Traversable      # interface
phpdoc PHP_EOL          # constant
phpdoc PDO::FETCH_ASSOC # object constant
phpdoc PDO::quote       # object method
phpdoc MCRYPT_MODE_ECB  # extension constant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment