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

1

6

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. :)

Destiny answered 30/7, 2014 at 19:17 Comment(8)
Most IDEs provide some shortcut code to generate PHPDocumentor stubs for you, although you still need to fill in the details yourself - if you're using Eclipse, take a look at this even if it is a bit old nowGent
@MarkBaker I'm familiar with PHPDocumenter and have used it in the past, but from my experience it simply repackages my code hints into pretty documentation, but not in a form that would support code-completion in the way that the standard.php file does. I'd love to be wrong though ;)Destiny
Au-contraire.... while PHPDocumentor itself simply takes those annotations and uses them to build documentation, it's those self-same annotations (often called docblocks) that most modern IDEs use for autocomplete and showing the arguments for the function/methodGent
@MarkBaker You're absolutely right, and I should have hinted at that ;) I'm quite anal about producing docblocks throughout our codebase, both for declarations as well as inline thought processes, reasoning, TODO's, FIXME's, etc. I use it everywhere. But as you can see in the example I provided, it's essentially the real code but with all of the guts removed, just the declarations and docblocks. I want a way to automatically do the same for my own code base.Destiny
Place your text cursor right above your function, class or whatever, type /** and press enter and Eclipse should generate a stub docblock for you; you can then write @ blocks inside the comments, using @ followed by ctrl+space to get auto-completion hints for the tagsGent
@MarkBaker I think you misunderstand, I have no problem creating docblocks, I'm quite versed in their use and creation. I want an external utility (like phpDocumentor) that will take all of my classes, and strip out everything except for the function declarations and their EXISTING docblocks. Look at the example in my question and it should be obvious what I mean.Destiny
Apologies, yes.... I completely misunderstoodGent
I understand your question, but why not creating your own standard.php file? Now the question arises on how do you auto-complete that file or how to let eclipse do that for you, but why not create an php script that does just that, with an admin interface connected to a database (could very be a API call MyAppDoc or something) that will write to that file whenever you call the API to document your code just the way you want?Phonolite
P
2

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.

Practise answered 22/10, 2014 at 13:31 Comment(3)
That's exactly what we're looking for, and perfect timing too as I was about to revisit this, it'd fallen off the priority list for a while. I'll package this in a dir tree iterator and put it into the repo I mentioned in the post, and I'd be happy to invite you on board if you're interested, but will certainly reference your contribution! Really appreciate your time to work that out!Destiny
Thanks mate! Always happy to help out the community.Practise
Hey, been a while but finally found the time to get this up and running, your code was a big help and I've attributed you and this SO question in the projects Aggregator.php class file. The output wasn't quite there so I modified a bit to get the correct Namespace / Class / Method nesting, but the basic intent is all still there. Take a look at the project here: github.com/ecommunities/Code-Hint-AggregatorDestiny

© 2022 - 2024 — McMap. All rights reserved.