20
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP 7.0.0α2 の例外の例外の継承関係を可視化してみた

Last updated at Posted at 2015-06-29

PHP7.0.0α2で例外の扱いが変わった らしいので継承関係を可視化してみた。

なお、php-build の 7.0.0alpha2 でやってます。

exception.php
<?php
$classes = array();

foreach (get_declared_classes() as $class) {
    if (is_a($class, 'Throwable', true)) {
        if (get_parent_class($class) !== false) {
            $classes[$class] = get_parent_class($class);
        }
    }
}

?>
digraph Exception {
    node [shape="box", fontname="sazanami gothic" fontsize="9"];
    graph [rankdir = RL, fontpath="/usr/share/fonts/japanese/TrueType/"];

    <?php foreach ($classes as $class => $parent): ?>

        <?= $class ?> -> <?= $parent ?>

    <?php endforeach; ?>

}
php exception.php | dot -Tpng > exception.png 

classes.png

インタフェースが書かれていないので Exception と Error が繋がっていません。


インタフェースも書いてみました。

exception.php
<?php
$classes = array();
$interfaces =  array();

function func(ReflectionClass $class)
{
    global $interfaces;
    $name = $class->getName();
    if (isset($interfaces[$name]) === false) {
        $interfaces[$name] = [];
        foreach ($class->getInterfaces() as $iface) {
            $interfaces[$name][] = $iface->getName();
            func($iface);
        }
    }
}

foreach (get_declared_classes() as $class) {
    if (is_a($class, 'Throwable', true)) {
        if (get_parent_class($class) !== false) {
            $classes[$class] = get_parent_class($class);
        }
        func(new ReflectionClass($class));
    }
}

?>
digraph Exception {
    node [shape="box", fontname="sazanami gothic" fontsize="9"];
    graph [rankdir = RL, fontpath="/usr/share/fonts/japanese/TrueType/"];

    <?php foreach ($classes as $class => $parent): ?>

        <?= $class ?> -> <?= $parent ?>

    <?php endforeach; ?>

    <?php foreach ($interfaces as $class => $arr): ?>
        <?php foreach ($arr as $iface): ?>

        <?= $class ?> -> <?= $iface ?> [style = dashed];

        <?php endforeach; ?>
    <?php endforeach; ?>

}
php exception.php | dot -Tpng > exception.png 

exception.png

間接的に実装しているインタフェースも ReflectionClass::getInterfaces() に出てくるのでめちゃくちゃわかりにくい図になってしまいました。。。


親が実装しているインタフェースは書かないようにした。

exception.php
<?php
$classes = array();
$interfaces =  array();

function func(ReflectionClass $class)
{
    global $interfaces;
    $name = $class->getName();
    if (isset($interfaces[$name]) === false) {
        $interfaces[$name] = [];
        foreach ($class->getInterfaces() as $iface) {
            if ($class->getParentClass()) {
                if ($class->getParentClass()->implementsInterface($iface)) {
                    continue;
                }
            }
            $interfaces[$name][] = $iface->getName();
            func($iface);
        }
    }
}

foreach (get_declared_classes() as $class) {
    if (is_a($class, 'Throwable', true)) {
        if (get_parent_class($class) !== false) {
            $classes[$class] = get_parent_class($class);
        }
        func(new ReflectionClass($class));
    }
}

?>
digraph Exception {
    node [shape="box", fontname="sazanami gothic" fontsize="9"];
    graph [rankdir = RL, fontpath="/usr/share/fonts/japanese/TrueType/"];

    <?php foreach ($classes as $class => $parent): ?>

        <?= $class ?> -> <?= $parent ?>

    <?php endforeach; ?>

    <?php foreach ($interfaces as $class => $arr): ?>
        <?php foreach ($arr as $iface): ?>

        <?= $class ?> -> <?= $iface ?> [style = dashed];

        <?php endforeach; ?>
    <?php endforeach; ?>

}
php exception.php | dot -Tpng > exception.png 

exception.png

20
22
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?