Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Conversation

@elkuku
Copy link

@elkuku elkuku commented Apr 4, 2014

Hello,
I'd like to use phpcpd from within another application and in conjunction with other CI tools like phpcs, phpunit etc. being installed using Composer.
I use the following code:

class MyApplication...
{
    someFunction()
    {
        $application = new \SebastianBergmann\PHPCPD\CLI\Application;

        $application->setAutoExit(false);

        $cloneCount = $application->run(
            new ArrayInput(
                ['values' => [
                    JPATH_ROOT . '/cli',
                    JPATH_ROOT . '/src'
                ]]
            )
        );
        // more code..
    }
}

which works fine except the fact that if clones are found phpcpd will exit() so my application "dies"...

So the idea here is to return the number of clones found (or zero) instead of hard exiting. I know this a a bit "unorthodox", but it works. The only limit here would be that only 255 clones will be displayed as this is the maximum error status, but well ;)

Another (and of course "cleaner") option would be to return 1, so the SymphonyApplication will exit with this status code (if autoExit isn't set to false).

BTW: here is a Travis log of our custom script invoking phpcs and phpunit using a similar technique described earlier: https://travis-ci.org/joomla/jissues/jobs/21904034
The scripts invoking those can be found here: https://github.com/joomla/jissues/tree/master/cli/Application/Command/Test
This is a Joomla! Application 😉

@elkuku elkuku mentioned this pull request Apr 4, 2014
@maglnet
Copy link

maglnet commented Apr 13, 2014

Hi,

since you're using phpcpd within php, using the detector class directly without using the console application could already solve your initial problem.
Something like this could help to get the number of clones from within php (untested):

use SebastianBergmann\PHPCPD\Detector\Detector;
use SebastianBergmann\PHPCPD\Detector\Strategy\DefaultStrategy;
use SebastianBergmann\FinderFacade\FinderFacade;
$finder = new FinderFacade(
    array('src/', 'cli/')
);
$files = $finder->findFiles();

$strategy = new DefaultStrategy();
$detector = new Detector($strategy);
$clones = $detector->copyPasteDetection(
    $files,
    $min_lines,
    $min_tokens,
    $input->getOption('fuzzy')
);
$number_of_clones = $clones->count();

Of course this doesn't give you the cli output, but you get the ClonesMap which you could use to generate your own summary.

Best regards,
Matthias

@elkuku
Copy link
Author

elkuku commented Apr 17, 2014

Nice solution, thanks for pointing it out.

I wonder if this could still be merged, since its consistent with other applications and the current doc blocks (e.g. returning something) and it should also be backward compatible 😉

@elkuku
Copy link
Author

elkuku commented Apr 17, 2014

So this would be a "won't fix" ?

@sebastianbergmann if the problem is the "unorthodox" exit status, I'd set this to just 0 or 1 if it helps in merging 😉

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants