Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to generate a code hint reference for my application like PHP's standard.php

Eclipse accomplishes PHP function/method hinting by placing all of PHP's function names and code hints into a file called standard.php and associates it to a project as a library(?). Just CTRL + Click any php function to bring it up.

Within standard.php, there is reference after reference for all of PHP's functions like so...

/**
 * Find whether the type of a variable is integer
 * @link http://www.php.net/manual/en/function.is-int.php
 * @param var mixed <p>
 * The variable being evaluated.
 * </p>
 * @return bool true if var is an integer,
 * false otherwise.
 */
function is_int ($var) {}

I would like to be able to provide something similar to my programmers covering our own application so that I can limit access to our actual software source code, but still give them the benefit of code hinting support and documentation.

Question: Is there a way in Eclipse to export or automatically generate a similar function reference capable of serving the same purpose as the PHP one in standard.php?


EDIT: We're in the early stages of creating a utility to do just this which we will put up to GitHub once it's far enough along.

We've created an empty repo at Github for it for the time being, so star it there if you're interested in getting a copy when it goes up. The repo can be found here: https://github.com/ecommunities/Code-Hint-Aggregator


UPDATE: It's taken a little while to find the time, but the GitHub project referenced above is now up and running and we can now parse an entire project and output a map of it's entire namespace / class / method structure. FYI, It's still in Alpha but it's worth a look. :)

like image 477
oucil Avatar asked Jul 30 '14 19:07

oucil


1 Answers

You can use Zend Framework's reflection packages, have a look at them here http://framework.zend.com/apidoc/2.1/namespaces/Zend.Code.html

Basically you need to do something like

<?php
use Zend\Code\Reflection\FileReflection;
use Zend\Code\Generator\MethodGenerator;

$path ='test/TestClass.php';

include_once $path;

$reflection = new FileReflection($path);

foreach ($reflection->getClasses() as $class) {
    $namespace = $class->getNamespaceName();
    $className = $class->getShortName();
    foreach ($class->getMethods() as $methodReflection) {
        $output = '';

        $method = MethodGenerator::fromReflection($methodReflection);
        $docblock = $method->getDocblock();
        if ($docblock) {
            $output .= $docblock->generate();
        }
        $params = implode(', ', array_map(function($item) {
            return $item->generate();
        }, $method->getParameters()));

        $output .= $namespace . ' ' . $className . '::' . $method->getName() . '(' . $params . ')';
        echo $output;
        echo PHP_EOL . PHP_EOL;
    }
}

When I run this on a test class that looks like this:

<?php
class TestClass
{
    /**
     * Lorem ipsum dolor sit amet
     *
     * @param string $foo kung-foo
     * @param array $bar  array of mars bars
     *
     * @return void
     */
    public function foo($foo, array $bar)
    {
    }

    public function bar($foo, $bar)
    {
    }
}

I get this output:

➜  reflection  php bin/parser.php
/**
 * Lorem ipsum dolor sit amet
 *
 * @param string $foo kung-foo
 * @param array $bar  array of mars bars
 *
 * @return void
 *
 */
 TestClass::foo($foo, array $bar)

 TestClass::bar($foo, $bar)

Which I think it's what you want.

like image 162
motanelu Avatar answered Nov 19 '22 18:11

motanelu