Issues (9)

src/Extensions/Annotatable.php (2 issues)

1
<?php
2
3
namespace SilverLeague\IDEAnnotator\Extensions;
4
5
use Psr\Container\NotFoundExceptionInterface;
6
use ReflectionException;
7
use SilverLeague\IDEAnnotator\DataObjectAnnotator;
8
use SilverLeague\IDEAnnotator\Helpers\AnnotatePermissionChecker;
9
use SilverStripe\Control\Director;
10
use SilverStripe\Core\Extension;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Dev\DevBuildController;
13
14
/**
15
 * Class Annotatable
16
 *
17
 * Annotate extension for the provided DataObjects for autocompletion purposes.
18
 * Start annotation, if skipannotation is not set and the annotator is enabled.
19
 *
20
 * @package IDEAnnotator/Extensions
21
 * @property DevBuildController|Annotatable $owner
22
 */
23
class Annotatable extends Extension
24
{
25
26
    /**
27
     * Keep track ot the annotation actions for extensions
28
     * An Extension can belong to many DataObjects.
29
     * This prevents that an Extension is ran twice on dev/build
30
     * @var array
31
     */
32
    public static $annotated_extensions = [];
33
    /**
34
     * @var DataObjectAnnotator
35
     */
36
    protected $annotator;
37
    /**
38
     * @var AnnotatePermissionChecker
39
     */
40
    protected $permissionChecker;
41
42
    /**
43
     * @config
44
     * Enables generation of docblocks on build
45
     *
46
     * @var bool
47
     */
48
    private static $annotate_on_build = true;
49
50
    /**
51
     * Annotated Controllers and Extensions
52
     * @throws NotFoundExceptionInterface
53
     * @throws ReflectionException
54
     */
55
    public function afterCallActionHandler()
56
    {
57
        if ($this->owner->config()->annotate_on_build) {
0 ignored issues
show
The method config() does not exist on SilverLeague\IDEAnnotator\Extensions\Annotatable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        if ($this->owner->/** @scrutinizer ignore-call */ config()->annotate_on_build) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
            $this->annotateModules();
59
        }
60
    }
61
62
    /**
63
     * Conditionally annotate this project's modules if enabled and not skipped
64
     *
65
     * @return bool Return true if annotation was successful
66
     * @throws NotFoundExceptionInterface
67
     * @throws ReflectionException
68
     */
69
    public function annotateModules()
70
    {
71
        $envIsAllowed = Director::isDev() && DataObjectAnnotator::config()->get('enabled');
72
        $skipAnnotation = $this->owner->getRequest()->getVar('skipannotation');
0 ignored issues
show
The method getRequest() does not exist on SilverLeague\IDEAnnotator\Extensions\Annotatable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        $skipAnnotation = $this->owner->/** @scrutinizer ignore-call */ getRequest()->getVar('skipannotation');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
74
        // Only instatiate things when we want to run it, this is for when the module is accidentally installed
75
        // on non-dev environments for example
76
        if ($skipAnnotation === null && $envIsAllowed) {
77
            $this->setUp();
78
79
            $this->displayMessage('Generating class docblocks', true, false);
80
81
            $modules = $this->permissionChecker->enabledModules();
82
            foreach ($modules as $module) {
83
                $this->annotator->annotateModule($module);
84
            }
85
86
            $this->displayMessage('Docblock generation finished!', true, true);
87
88
            return true;
89
        }
90
91
        return false;
92
    }
93
94
    /**
95
     * Annotatable setup.
96
     * This is theoretically a constructor, but to save memory we're using setup
97
     * called from {@see afterCallActionHandler}
98
     * @throws NotFoundExceptionInterface
99
     */
100
    public function setUp()
101
    {
102
        $this->annotator = Injector::inst()->get(DataObjectAnnotator::class);
103
        $this->permissionChecker = Injector::inst()->get(AnnotatePermissionChecker::class);
104
    }
105
106
    /**
107
     * @param string $message
108
     * @param bool   $heading
109
     * @param bool   $end
110
     */
111
    public function displayMessage($message, $heading = false, $end = false)
112
    {
113
        if ($heading) {
114
            if (!$end) {
115
                echo Director::is_cli() ?
116
                    strtoupper("\n$message\n\n") :
117
                    "<div class='build'><p><b>$message</b><ul>";
118
            } else {
119
                echo Director::is_cli() ? strtoupper("\n" . $message) : "</ul><p><b>$message</b></b></div>";
120
            }
121
        } else {
122
            echo Director::is_cli() ? "\n$message\n\n" : "<li>$message</li>";
123
        }
124
    }
125
126
    /**
127
     * @return DataObjectAnnotator
128
     */
129
    public function getAnnotator()
130
    {
131
        return $this->annotator;
132
    }
133
134
    /**
135
     * @return AnnotatePermissionChecker
136
     */
137
    public function getPermissionChecker()
138
    {
139
        return $this->permissionChecker;
140
    }
141
}
142