sonata-project / SonataClassificationBundle
Showing 21 of 44 files from the diff.

@@ -48,7 +48,7 @@
Loading
48 48
49 49
    public function __toString(): string
50 50
    {
51 -
        return $this->getName() ?: 'n/a';
51 +
        return $this->getName() ?? 'n/a';
52 52
    }
53 53
54 54
    public function setName(?string $name): void
@@ -150,13 +150,13 @@
Loading
150 150
    public function removeChild(CategoryInterface $childToDelete): void
151 151
    {
152 152
        foreach ($this->getChildren() as $pos => $child) {
153 -
            if ($childToDelete->getId() && $child->getId() === $childToDelete->getId()) {
153 +
            if (null !== $childToDelete->getId() && $child->getId() === $childToDelete->getId()) {
154 154
                unset($this->children[$pos]);
155 155
156 156
                return;
157 157
            }
158 158
159 -
            if (!$childToDelete->getId() && $child === $childToDelete) {
159 +
            if (null === $childToDelete->getId() && $child === $childToDelete) {
160 160
                unset($this->children[$pos]);
161 161
162 162
                return;
@@ -187,7 +187,7 @@
Loading
187 187
    {
188 188
        $this->parent = $parent;
189 189
190 -
        if (!$nested && $parent) {
190 +
        if (!$nested && null !== $parent) {
191 191
            $parent->addChild($this, true);
192 192
        }
193 193
    }

@@ -93,7 +93,7 @@
Loading
93 93
94 94
            \assert(null !== $catContext);
95 95
96 -
            $choices[sprintf('%s (%s)', $category->getName(), $catContext->getId())] = $category->getId();
96 +
            $choices[sprintf('%s (%s)', $category->getName() ?? '', $catContext->getId() ?? '')] = $category->getId();
97 97
98 98
            $this->visitChild($category, $choices);
99 99
        }

@@ -156,8 +156,9 @@
Loading
156 156
157 157
    public function load(BlockInterface $block): void
158 158
    {
159 -
        if (is_numeric($block->getSetting('collectionId'))) {
160 -
            $block->setSetting('collectionId', $this->getCollection($block->getSetting('collectionId')));
159 +
        $collectionId = $block->getSetting('collectionId');
160 +
        if (\is_int($collectionId) || \is_string($collectionId)) {
161 +
            $block->setSetting('collectionId', $this->getCollection($collectionId));
161 162
        }
162 163
    }
163 164
@@ -179,8 +180,8 @@
Loading
179 180
    }
180 181
181 182
    /**
182 -
     * @param CollectionInterface|int $id
183 -
     * @param mixed                   $default
183 +
     * @param CollectionInterface|int|string|null $id
184 +
     * @param mixed                               $default
184 185
     */
185 186
    final protected function getCollection($id, $default = null): ?CollectionInterface
186 187
    {
@@ -188,7 +189,7 @@
Loading
188 189
            return $id;
189 190
        }
190 191
191 -
        if (is_numeric($id)) {
192 +
        if (null !== $id) {
192 193
            return $this->collectionManager->find($id);
193 194
        }
194 195
@@ -203,7 +204,9 @@
Loading
203 204
    {
204 205
        $block->setSetting(
205 206
            'collectionId',
206 -
            \is_object($block->getSetting('collectionId')) ? $block->getSetting('collectionId')->getId() : null
207 +
            $block->getSetting('collectionId') instanceof CollectionInterface
208 +
                ? $block->getSetting('collectionId')->getId()
209 +
                : null
207 210
        );
208 211
    }
209 212
}

@@ -51,6 +51,7 @@
Loading
51 51
52 52
    public function configure(): void
53 53
    {
54 +
        \assert(null !== static::$defaultDescription);
54 55
        $this->setDescription(static::$defaultDescription);
55 56
    }
56 57
@@ -62,7 +63,7 @@
Loading
62 63
            'id' => ContextInterface::DEFAULT_CONTEXT,
63 64
        ]);
64 65
65 -
        if (!$defaultContext) {
66 +
        if (null === $defaultContext) {
66 67
            $output->writeln(' > default context is missing, creating one');
67 68
            $defaultContext = $this->contextManager->create();
68 69
            $defaultContext->setId(ContextInterface::DEFAULT_CONTEXT);
@@ -77,11 +78,14 @@
Loading
77 78
        $output->writeln('2. Find tag without default context');
78 79
79 80
        foreach ($this->tagManager->findBy([]) as $tag) {
80 -
            if ($tag->getContext()) {
81 +
            if (null !== $tag->getContext()) {
81 82
                continue;
82 83
            }
83 84
84 -
            $output->writeln(sprintf(' > attach default context to tag: %s (%s)', $tag->getSlug(), $tag->getId()));
85 +
            $tagId = $tag->getId();
86 +
            \assert(null !== $tagId);
87 +
88 +
            $output->writeln(sprintf(' > attach default context to tag: %s (%s)', $tag->getSlug() ?? '', $tagId));
85 89
            $tag->setContext($defaultContext);
86 90
87 91
            $this->tagManager->save($tag);
@@ -90,11 +94,14 @@
Loading
90 94
        $output->writeln('3. Find collection without default context');
91 95
92 96
        foreach ($this->collectionManager->findBy([]) as $collection) {
93 -
            if ($collection->getContext()) {
97 +
            if (null !== $collection->getContext()) {
94 98
                continue;
95 99
            }
96 100
97 -
            $output->writeln(sprintf(' > attach default context to collection: %s (%s)', $collection->getSlug(), $collection->getId()));
101 +
            $collectionId = $collection->getId();
102 +
            \assert(null !== $collectionId);
103 +
104 +
            $output->writeln(sprintf(' > attach default context to collection: %s (%s)', $collection->getSlug() ?? '', $collectionId));
98 105
            $collection->setContext($defaultContext);
99 106
100 107
            $this->collectionManager->save($collection);
@@ -103,11 +110,14 @@
Loading
103 110
        $output->writeln('3. Find category without default context');
104 111
105 112
        foreach ($this->categoryManager->findBy([]) as $category) {
106 -
            if ($category->getContext()) {
113 +
            if (null !== $category->getContext()) {
107 114
                continue;
108 115
            }
109 116
110 -
            $output->writeln(sprintf(' > attach default context to collection: %s (%s)', $category->getSlug(), $category->getId()));
117 +
            $categoryId = $category->getId();
118 +
            \assert(null !== $categoryId);
119 +
120 +
            $output->writeln(sprintf(' > attach default context to collection: %s (%s)', $category->getSlug() ?? '', $categoryId));
111 121
            $category->setContext($defaultContext);
112 122
113 123
            $this->categoryManager->save($category);

@@ -16,7 +16,6 @@
Loading
16 16
use Sonata\AdminBundle\Admin\AbstractAdmin;
17 17
use Sonata\AdminBundle\Datagrid\DatagridMapper;
18 18
use Sonata\ClassificationBundle\Model\ContextAwareInterface;
19 -
use Sonata\ClassificationBundle\Model\ContextInterface;
20 19
use Sonata\ClassificationBundle\Model\ContextManagerInterface;
21 20
22 21
/**
@@ -39,11 +38,11 @@
Loading
39 38
40 39
    protected function alterNewInstance(object $object): void
41 40
    {
42 -
        if ($contextId = $this->getPersistentParameter('context')) {
41 +
        $contextId = $this->getPersistentParameter('context', '');
42 +
        if ('' !== $contextId) {
43 43
            $context = $this->contextManager->find($contextId);
44 44
45 -
            if (!$context) {
46 -
                /** @var ContextInterface $context */
45 +
            if (null !== $context) {
47 46
                $context = $this->contextManager->create();
48 47
                $context->setEnabled(true);
49 48
                $context->setId($contextId);

@@ -31,7 +31,7 @@
Loading
31 31
32 32
    public function __toString(): string
33 33
    {
34 -
        return $this->getName() ?: 'n/a';
34 +
        return $this->getName() ?? 'n/a';
35 35
    }
36 36
37 37
    public function setName(?string $name): void
@@ -100,7 +100,7 @@
Loading
100 100
    {
101 101
        $text = Slugify::create()->slugify($text);
102 102
103 -
        if (empty($text)) {
103 +
        if ('' === $text) {
104 104
            return 'n-a';
105 105
        }
106 106

@@ -76,9 +76,13 @@
Loading
76 76
    final protected function getContextChoices(): array
77 77
    {
78 78
        $contextChoices = [];
79 +
79 80
        /* @var ContextInterface $context */
80 81
        foreach ($this->contextManager->findAll() as $context) {
81 -
            $contextChoices[(string) $context->getId()] = (string) $context->getName();
82 +
            $contextId = $context->getId();
83 +
            \assert(null !== $contextId);
84 +
85 +
            $contextChoices[$contextId] = $context->getName() ?? '';
82 86
        }
83 87
84 88
        return $contextChoices;

@@ -65,7 +65,11 @@
Loading
65 65
        $choices = [];
66 66
67 67
        foreach ($categories as $category) {
68 -
            $choices[$category->getId()] = sprintf('%s (%s)', $category->getName(), $category->getContext()->getId());
68 +
            $context = $category->getContext();
69 +
            $categoryId = $category->getId();
70 +
            \assert(null !== $context && null !== $categoryId);
71 +
72 +
            $choices[$categoryId] = sprintf('%s (%s)', $category->getName() ?? '', $context->getId() ?? '');
69 73
70 74
            $this->childWalker($category, $options, $choices);
71 75
        }
@@ -93,11 +97,14 @@
Loading
93 97
        }
94 98
95 99
        foreach ($category->getChildren() as $child) {
96 -
            if ($options['category'] && $options['category']->getId() === $child->getId()) {
100 +
            $childId = $child->getId();
101 +
            \assert(null !== $childId);
102 +
103 +
            if ($options['category'] && $options['category']->getId() === $childId) {
97 104
                continue;
98 105
            }
99 106
100 -
            $choices[$child->getId()] = sprintf('%s %s', str_repeat('-', 1 * $level), $child->getName());
107 +
            $choices[$childId] = sprintf('%s %s', str_repeat('-', 1 * $level), $child->getName() ?? '');
101 108
102 109
            $this->childWalker($child, $options, $choices, $level + 1);
103 110
        }

@@ -73,7 +73,7 @@
Loading
73 73
    }
74 74
75 75
    /**
76 -
     * @return array<string, int>
76 +
     * @return array<string, int|string>
77 77
     */
78 78
    private function getChoices(): array
79 79
    {
@@ -88,7 +88,10 @@
Loading
88 88
        $choices = [];
89 89
90 90
        foreach ($collections as $collection) {
91 -
            $choices[(string) $collection] = $collection->getId();
91 +
            $id = $collection->getId();
92 +
            \assert(null !== $id);
93 +
94 +
            $choices[(string) $collection] = $id;
92 95
        }
93 96
94 97
        return $choices;

@@ -19,8 +19,6 @@
Loading
19 19
use Sonata\ClassificationBundle\Model\ContextInterface;
20 20
use Sonata\ClassificationBundle\Model\ContextManagerInterface;
21 21
use Sonata\Doctrine\Document\BaseDocumentManager;
22 -
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\Pager;
23 -
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
24 22
25 23
/**
26 24
 * @phpstan-extends BaseDocumentManager<CategoryInterface>
@@ -35,7 +33,7 @@
Loading
35 33
    protected ContextManagerInterface $contextManager;
36 34
37 35
    /**
38 -
     * @phpstan-param class-string $class
36 +
     * @phpstan-param class-string<CategoryInterface> $class
39 37
     */
40 38
    public function __construct(string $class, ManagerRegistry $registry, ContextManagerInterface $contextManager)
41 39
    {
@@ -45,72 +43,59 @@
Loading
45 43
        $this->categories = [];
46 44
    }
47 45
48 -
    public function getRootCategoriesPager(int $page = 1, int $limit = 25, array $criteria = []): Pager
49 -
    {
50 -
        $queryBuilder = $this->getDocumentManager()
51 -
            ->createQueryBuilder($this->getClass())
52 -
            ->field('parent')
53 -
            ->equals(null);
54 -
55 -
        $pager = new Pager($limit);
56 -
        $pager->setQuery(new ProxyQuery($queryBuilder));
57 -
        $pager->setPage($page);
58 -
        $pager->init();
59 -
60 -
        return $pager;
61 -
    }
62 -
63 -
    public function getSubCategoriesPager($categoryId, int $page = 1, int $limit = 25, array $criteria = []): Pager
64 -
    {
65 -
        $queryBuilder = $this->getDocumentManager()
66 -
            ->createQueryBuilder($this->getClass())
67 -
            ->field('parent')
68 -
            ->equals($categoryId);
69 -
70 -
        $pager = new Pager($limit);
71 -
        $pager->setQuery(new ProxyQuery($queryBuilder));
72 -
        $pager->setPage($page);
73 -
        $pager->init();
74 -
75 -
        return $pager;
76 -
    }
77 -
78 46
    public function getRootCategoryWithChildren(CategoryInterface $category): CategoryInterface
79 47
    {
80 -
        if (null === $category->getContext()) {
48 +
        $context = $category->getContext();
49 +
        if (null === $context) {
81 50
            throw new \InvalidArgumentException(sprintf(
82 51
                'Context of category "%s" cannot be null.',
83 -
                $category->getId()
52 +
                $category->getId() ?? ''
53 +
            ));
54 +
        }
55 +
56 +
        $contextId = $context->getId();
57 +
        if (null === $contextId) {
58 +
            throw new \InvalidArgumentException(sprintf(
59 +
                'Context of category "%s" must have an not null identifier.',
60 +
                $category->getId() ?? ''
84 61
            ));
85 62
        }
63 +
86 64
        if (null !== $category->getParent()) {
87 65
            throw new \InvalidArgumentException('Method can be called only for root categories.');
88 66
        }
89 67
90 -
        $context = $category->getContext();
91 -
92 68
        $this->loadCategories($context);
93 69
94 -
        foreach ($this->categories[$context->getId()] as $contextRootCategory) {
70 +
        foreach ($this->categories[$contextId] as $contextRootCategory) {
95 71
            if ($category->getId() === $contextRootCategory->getId()) {
96 72
                return $contextRootCategory;
97 73
            }
98 74
        }
99 75
100 -
        throw new \InvalidArgumentException(sprintf('Category "%s" does not exist.', $category->getId()));
76 +
        throw new \InvalidArgumentException(sprintf('Category "%s" does not exist.', $category->getId() ?? ''));
101 77
    }
102 78
103 79
    public function getRootCategoriesForContext(?ContextInterface $context = null): array
104 80
    {
105 81
        $context = $this->getContext($context);
106 82
83 +
        $contextId = $context->getId();
84 +
        if (null === $contextId) {
85 +
            throw new \InvalidArgumentException(sprintf(
86 +
                'Context "%s" must have an not null identifier.',
87 +
                $context->getName() ?? ''
88 +
            ));
89 +
        }
90 +
107 91
        $this->loadCategories($context);
108 92
109 -
        return $this->categories[$context->getId()];
93 +
        return $this->categories[$contextId];
110 94
    }
111 95
112 96
    public function getAllRootCategories(bool $loadChildren = true): array
113 97
    {
98 +
        /** @var CategoryInterface[] $rootCategories */
114 99
        $rootCategories = $this->getDocumentManager()
115 100
            ->createQueryBuilder($this->getClass())
116 101
            ->field('parent')
@@ -124,7 +109,7 @@
Loading
124 109
            if (null === $category->getContext()) {
125 110
                throw new \LogicException(sprintf(
126 111
                    'Context of category "%s" cannot be null.',
127 -
                    $category->getId()
112 +
                    $category->getId() ?? ''
128 113
                ));
129 114
            }
130 115
@@ -141,7 +126,10 @@
Loading
141 126
        $splitCategories = [];
142 127
143 128
        foreach ($rootCategories as $category) {
144 -
            $splitCategories[(string) $category->getContext()->getId()][] = $category;
129 +
            $catContext = $category->getContext();
130 +
            \assert(null !== $catContext);
131 +
132 +
            $splitCategories[(string) $catContext->getId()][] = $category;
145 133
        }
146 134
147 135
        return $splitCategories;
@@ -177,10 +165,12 @@
Loading
177 165
     */
178 166
    protected function loadCategories(ContextInterface $context): void
179 167
    {
180 -
        if (\array_key_exists($context->getId(), $this->categories)) {
168 +
        $contextId = $context->getId();
169 +
        if (null === $contextId || \array_key_exists($contextId, $this->categories)) {
181 170
            return;
182 171
        }
183 172
173 +
        /** @var CategoryInterface[] $categories */
184 174
        $categories = $this->getDocumentManager()
185 175
            ->createQueryBuilder($this->getClass())
186 176
            ->field('context')
@@ -205,7 +195,7 @@
Loading
205 195
        $root = null;
206 196
207 197
        foreach ($categories as $pos => $category) {
208 -
            if (0 === $pos && $category->getParent()) {
198 +
            if (0 === $pos && null !== $category->getParent()) {
209 199
                throw new \LogicException('The first category must be the root.');
210 200
            }
211 201
@@ -213,30 +203,29 @@
Loading
213 203
                $root = $category;
214 204
            }
215 205
216 -
            $this->categories[(string) $context->getId()][(int) $category->getId()] = $category;
206 +
            $categoryId = $category->getId();
207 +
            \assert(null !== $categoryId);
208 +
            $this->categories[$contextId][$categoryId] = $category;
217 209
218 210
            $parent = $category->getParent();
219 -
220 -
            $category->disableChildrenLazyLoading();
221 -
222 -
            if ($parent) {
211 +
            if (null !== $parent) {
223 212
                $parent->addChild($category);
224 213
            }
225 214
        }
226 215
227 216
        if (null !== $root) {
228 -
            $this->categories[(string) $context->getId()] = [
217 +
            $this->categories[$contextId] = [
229 218
                0 => $root,
230 219
            ];
231 220
        }
232 221
    }
233 222
234 223
    /**
235 -
     * @param ContextInterface|string $context
224 +
     * @param ContextInterface|string|null $context
236 225
     */
237 226
    private function getContext($context): ContextInterface
238 227
    {
239 -
        if (empty($context)) {
228 +
        if (null === $context) {
240 229
            $context = ContextInterface::DEFAULT_CONTEXT;
241 230
        }
242 231

@@ -31,7 +31,7 @@
Loading
31 31
32 32
    public function __toString(): string
33 33
    {
34 -
        return $this->getName() ?: 'n/a';
34 +
        return $this->getName() ?? 'n/a';
35 35
    }
36 36
37 37
    public function setName(?string $name): void

@@ -156,8 +156,9 @@
Loading
156 156
157 157
    public function load(BlockInterface $block): void
158 158
    {
159 -
        if (is_numeric($block->getSetting('tagId'))) {
160 -
            $block->setSetting('tagId', $this->getTag($block->getSetting('tagId')));
159 +
        $tagId = $block->getSetting('tagId');
160 +
        if (\is_int($tagId) || \is_string($tagId)) {
161 +
            $block->setSetting('tagId', $this->getTag($tagId));
161 162
        }
162 163
    }
163 164
@@ -179,8 +180,8 @@
Loading
179 180
    }
180 181
181 182
    /**
182 -
     * @param TagInterface|int $id
183 -
     * @param mixed            $default
183 +
     * @param TagInterface|int|string|null $id
184 +
     * @param mixed                        $default
184 185
     */
185 186
    final protected function getTag($id, $default = null): ?TagInterface
186 187
    {
@@ -188,7 +189,7 @@
Loading
188 189
            return $id;
189 190
        }
190 191
191 -
        if (is_numeric($id)) {
192 +
        if (null !== $id) {
192 193
            return $this->tagManager->find($id);
193 194
        }
194 195
@@ -203,7 +204,9 @@
Loading
203 204
    {
204 205
        $block->setSetting(
205 206
            'tagId',
206 -
            \is_object($block->getSetting('tagId')) ? $block->getSetting('tagId')->getId() : null
207 +
            $block->getSetting('tagId') instanceof TagInterface
208 +
                ? $block->getSetting('tagId')->getId()
209 +
                : null
207 210
        );
208 211
    }
209 212
}

@@ -40,21 +40,22 @@
Loading
40 40
41 41
    public function listAction(Request $request): Response
42 42
    {
43 -
        if (!$request->get('filter') && !$request->get('filters')) {
43 +
        if (null === $request->get('filter') && null === $request->get('filters')) {
44 44
            return new RedirectResponse($this->admin->generateUrl('tree', $request->query->all()));
45 45
        }
46 46
47 -
        if ($listMode = $request->get('_list_mode')) {
47 +
        $listMode = $request->get('_list_mode');
48 +
        if (null !== $listMode) {
48 49
            $this->admin->setListMode($listMode);
49 50
        }
50 51
51 52
        $datagrid = $this->admin->getDatagrid();
52 53
        $datagridValues = $datagrid->getValues();
53 54
54 -
        $datagridContextIsSet = isset($datagridValues['context']['value']) && !empty($datagridValues['context']['value']);
55 +
        $datagridContextIsSet = isset($datagridValues['context']['value']) && '' !== $datagridValues['context']['value'];
55 56
56 57
        //ignore `context` persistent parameter if datagrid `context` value is set
57 -
        if ($this->admin->getPersistentParameter('context') && !$datagridContextIsSet) {
58 +
        if ('' !== $this->admin->getPersistentParameter('context', '') && !$datagridContextIsSet) {
58 59
            $datagrid->setValue('context', null, $this->admin->getPersistentParameter('context'));
59 60
        }
60 61
@@ -74,9 +75,16 @@
Loading
74 75
    public function treeAction(Request $request): Response
75 76
    {
76 77
        $categoryManager = $this->get('sonata.classification.manager.category');
77 -
        $currentContext = false;
78 -
        if ($contextId = $request->get('context')) {
79 -
            $currentContext = $this->get('sonata.classification.manager.context')->find($contextId);
78 +
        \assert($categoryManager instanceof CategoryManagerInterface);
79 +
80 +
        $currentContext = null;
81 +
82 +
        $contextId = $request->get('context');
83 +
        if (null !== $contextId) {
84 +
            $contextManager = $this->get('sonata.classification.manager.context');
85 +
            \assert($contextManager instanceof ContextManagerInterface);
86 +
87 +
            $currentContext = $contextManager->find($contextId);
80 88
        }
81 89
82 90
        // all root categories.
@@ -85,17 +93,19 @@
Loading
85 93
        // root categories inside the current context
86 94
        $currentCategories = [];
87 95
88 -
        if (!$currentContext && !empty($rootCategoriesSplitByContexts)) {
96 +
        if (null === $currentContext && [] !== $rootCategoriesSplitByContexts) {
89 97
            $currentCategories = current($rootCategoriesSplitByContexts);
98 +
            \assert([] !== $currentCategories);
90 99
            $currentContext = current($currentCategories)->getContext();
91 -
        } else {
92 -
            foreach ($rootCategoriesSplitByContexts as $contextId => $contextCategories) {
93 -
                if ($currentContext->getId() !== $contextId) {
100 +
        } elseif (null !== $currentContext) {
101 +
            foreach ($rootCategoriesSplitByContexts as $id => $contextCategories) {
102 +
                if ($currentContext->getId() !== $id) {
94 103
                    continue;
95 104
                }
96 105
97 106
                foreach ($contextCategories as $category) {
98 -
                    if ($currentContext->getId() !== $category->getContext()->getId()) {
107 +
                    $catContext = $category->getContext();
108 +
                    if (null === $catContext || $currentContext->getId() !== $catContext->getId()) {
99 109
                        continue;
100 110
                    }
101 111
@@ -106,8 +116,8 @@
Loading
106 116
107 117
        $datagrid = $this->admin->getDatagrid();
108 118
109 -
        if ($this->admin->getPersistentParameter('context')) {
110 -
            $datagrid->setValue('context', null, $this->admin->getPersistentParameter('context'));
119 +
        if ('' !== $this->admin->getPersistentParameter('context', '')) {
120 +
            $datagrid->setValue('context', null, $this->admin->getPersistentParameter('context', ''));
111 121
        }
112 122
113 123
        $formView = $datagrid->getForm()->createView();

@@ -60,10 +60,9 @@
Loading
60 60
                ->equals($enabled);
61 61
        }
62 62
63 +
        /** @var CollectionInterface[] $result */
63 64
        $result = $queryBuilder->getQuery()->execute();
64 65
65 -
        \assert(null !== $result);
66 -
67 66
        return $result;
68 67
    }
69 68
}

@@ -60,10 +60,9 @@
Loading
60 60
                ->equals($enabled);
61 61
        }
62 62
63 +
        /** @var TagInterface[] $result */
63 64
        $result = $queryBuilder->getQuery()->execute();
64 65
65 -
        \assert(null !== $result);
66 -
67 66
        return $result;
68 67
    }
69 68
}

@@ -27,7 +27,7 @@
Loading
27 27
28 28
    public function __toString(): string
29 29
    {
30 -
        return $this->getName() ?: 'n/a';
30 +
        return $this->getName() ?? 'n/a';
31 31
    }
32 32
33 33
    public function setName(?string $name): void

@@ -32,7 +32,7 @@
Loading
32 32
            ->add('name')
33 33
            ->add('context');
34 34
35 -
        if ($this->hasSubject() && $this->getSubject()->getId()) {
35 +
        if ($this->hasSubject() && null !== $this->getSubject()->getId()) {
36 36
            $form->add('slug');
37 37
        }
38 38

@@ -20,8 +20,6 @@
Loading
20 20
use Sonata\ClassificationBundle\Model\ContextInterface;
21 21
use Sonata\ClassificationBundle\Model\ContextManagerInterface;
22 22
use Sonata\Doctrine\Entity\BaseEntityManager;
23 -
use Sonata\DoctrineORMAdminBundle\Datagrid\Pager;
24 -
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
25 23
26 24
/**
27 25
 * @phpstan-extends BaseEntityManager<CategoryInterface>
@@ -46,65 +44,37 @@
Loading
46 44
        $this->categories = [];
47 45
    }
48 46
49 -
    /**
50 -
     * Returns a pager to iterate over the root category.
51 -
     */
52 -
    public function getRootCategoriesPager(int $page = 1, int $limit = 25, array $criteria = []): Pager
53 -
    {
54 -
        $page = 0 === $page ? 1 : $page;
55 -
56 -
        $queryBuilder = $this->getEntityManager()->createQueryBuilder()
57 -
            ->select('c')
58 -
            ->from($this->class, 'c')
59 -
            ->andWhere('c.parent IS NULL');
60 -
61 -
        $pager = new Pager($limit);
62 -
        $pager->setQuery(new ProxyQuery($queryBuilder));
63 -
        $pager->setPage($page);
64 -
        $pager->init();
65 -
66 -
        return $pager;
67 -
    }
68 -
69 -
    public function getSubCategoriesPager($categoryId, int $page = 1, int $limit = 25, array $criteria = []): Pager
70 -
    {
71 -
        $queryBuilder = $this->getEntityManager()->createQueryBuilder()
72 -
            ->select('c')
73 -
            ->from($this->class, 'c')
74 -
            ->where('c.parent = :categoryId')
75 -
            ->setParameter('categoryId', $categoryId);
76 -
77 -
        $pager = new Pager($limit);
78 -
        $pager->setQuery(new ProxyQuery($queryBuilder));
79 -
        $pager->setPage($page);
80 -
        $pager->init();
81 -
82 -
        return $pager;
83 -
    }
84 -
85 47
    public function getRootCategoryWithChildren(CategoryInterface $category): CategoryInterface
86 48
    {
87 -
        if (null === $category->getContext()) {
49 +
        $context = $category->getContext();
50 +
        if (null === $context) {
88 51
            throw new \InvalidArgumentException(sprintf(
89 52
                'Context of category "%s" cannot be null.',
90 -
                $category->getId()
53 +
                $category->getId() ?? ''
54 +
            ));
55 +
        }
56 +
57 +
        $contextId = $context->getId();
58 +
        if (null === $contextId) {
59 +
            throw new \InvalidArgumentException(sprintf(
60 +
                'Context of category "%s" must have an not null identifier.',
61 +
                $category->getId() ?? ''
91 62
            ));
92 63
        }
64 +
93 65
        if (null !== $category->getParent()) {
94 66
            throw new \InvalidArgumentException('Method can be called only for root categories.');
95 67
        }
96 68
97 -
        $context = $category->getContext();
98 -
99 69
        $this->loadCategories($context);
100 70
101 -
        foreach ($this->categories[$context->getId()] as $contextRootCategory) {
71 +
        foreach ($this->categories[$contextId] as $contextRootCategory) {
102 72
            if ($category->getId() === $contextRootCategory->getId()) {
103 73
                return $contextRootCategory;
104 74
            }
105 75
        }
106 76
107 -
        throw new \InvalidArgumentException(sprintf('Category "%s" does not exist.', $category->getId()));
77 +
        throw new \InvalidArgumentException(sprintf('Category "%s" does not exist.', $category->getId() ?? ''));
108 78
    }
109 79
110 80
    public function getRootCategoriesForContext(?ContextInterface $context = null): array
@@ -113,13 +83,22 @@
Loading
113 83
            $context = $this->getContext();
114 84
        }
115 85
86 +
        $contextId = $context->getId();
87 +
        if (null === $contextId) {
88 +
            throw new \InvalidArgumentException(sprintf(
89 +
                'Context "%s" must have an not null identifier.',
90 +
                $context->getName() ?? ''
91 +
            ));
92 +
        }
93 +
116 94
        $this->loadCategories($context);
117 95
118 -
        return $this->categories[$context->getId()];
96 +
        return $this->categories[$contextId];
119 97
    }
120 98
121 99
    public function getAllRootCategories(bool $loadChildren = true): array
122 100
    {
101 +
        /** @var CategoryInterface[] $rootCategories */
123 102
        $rootCategories = $this->getRepository()
124 103
            ->createQueryBuilder('c')
125 104
            ->where('c.parent IS NULL')
@@ -132,7 +111,7 @@
Loading
132 111
            if (null === $category->getContext()) {
133 112
                throw new \LogicException(sprintf(
134 113
                    'Context of category "%s" cannot be null.',
135 -
                    $category->getId()
114 +
                    $category->getId() ?? ''
136 115
                ));
137 116
            }
138 117
@@ -181,15 +160,17 @@
Loading
181 160
     */
182 161
    protected function loadCategories(ContextInterface $context): void
183 162
    {
184 -
        if (\array_key_exists($context->getId(), $this->categories)) {
163 +
        $contextId = $context->getId();
164 +
        if (null === $contextId || \array_key_exists($contextId, $this->categories)) {
185 165
            return;
186 166
        }
187 167
168 +
        /** @var CategoryInterface[] $categories */
188 169
        $categories = $this->getRepository()
189 170
            ->createQueryBuilder('c')
190 171
            ->where('c.context = :context')
191 172
            ->orderBy('c.parent')
192 -
            ->setParameter('context', $context->getId())
173 +
            ->setParameter('context', $contextId)
193 174
            ->getQuery()
194 175
            ->getResult();
195 176
@@ -212,18 +193,17 @@
Loading
212 193
                $rootCategories[] = $category;
213 194
            }
214 195
215 -
            $this->categories[(string) $context->getId()][$category->getId()] = $category;
196 +
            $categoryId = $category->getId();
197 +
            \assert(null !== $categoryId);
198 +
            $this->categories[$contextId][$categoryId] = $category;
216 199
217 200
            $parent = $category->getParent();
218 -
219 -
            $category->disableChildrenLazyLoading();
220 -
221 -
            if ($parent) {
201 +
            if (null !== $parent) {
222 202
                $parent->addChild($category);
223 203
            }
224 204
        }
225 205
226 -
        $this->categories[(string) $context->getId()] = $rootCategories;
206 +
        $this->categories[$contextId] = $rootCategories;
227 207
    }
228 208
229 209
    private function getContext(): ContextInterface

@@ -29,6 +29,11 @@
Loading
29 29
 */
30 30
final class Configuration implements ConfigurationInterface
31 31
{
32 +
    /**
33 +
     * @psalm-suppress PossiblyNullReference, PossiblyUndefinedMethod
34 +
     *
35 +
     * @see https://github.com/psalm/psalm-plugin-symfony/issues/174
36 +
     */
32 37
    public function getConfigTreeBuilder(): TreeBuilder
33 38
    {
34 39
        $treeBuilder = new TreeBuilder('sonata_classification');

@@ -48,7 +48,7 @@
Loading
48 48
49 49
    public function loadChoicesForValues(array $values, $value = null): array
50 50
    {
51 -
        if (empty($values)) {
51 +
        if ([] === $values) {
52 52
            return [];
53 53
        }
54 54
@@ -57,7 +57,7 @@
Loading
57 57
58 58
    public function loadValuesForChoices(array $choices, $value = null): array
59 59
    {
60 -
        if (empty($choices)) {
60 +
        if ([] === $choices) {
61 61
            return [];
62 62
        }
63 63

@@ -155,8 +155,9 @@
Loading
155 155
156 156
    public function load(BlockInterface $block): void
157 157
    {
158 -
        if (is_numeric($block->getSetting('categoryId'))) {
159 -
            $block->setSetting('categoryId', $this->getCategory($block->getSetting('categoryId')));
158 +
        $categoryId = $block->getSetting('categoryId');
159 +
        if (\is_int($categoryId) || \is_string($categoryId)) {
160 +
            $block->setSetting('categoryId', $this->getCategory($categoryId));
160 161
        }
161 162
    }
162 163
@@ -178,8 +179,8 @@
Loading
178 179
    }
179 180
180 181
    /**
181 -
     * @param CategoryInterface|int $id
182 -
     * @param mixed                 $default
182 +
     * @param CategoryInterface|int|string|null $id
183 +
     * @param mixed                             $default
183 184
     */
184 185
    final protected function getCategory($id, $default = null): ?CategoryInterface
185 186
    {
@@ -187,7 +188,7 @@
Loading
187 188
            return $id;
188 189
        }
189 190
190 -
        if (is_numeric($id)) {
191 +
        if (null !== $id) {
191 192
            return $this->categoryManager->find($id);
192 193
        }
193 194
@@ -202,7 +203,9 @@
Loading
202 203
    {
203 204
        $block->setSetting(
204 205
            'categoryId',
205 -
            \is_object($block->getSetting('categoryId')) ? $block->getSetting('categoryId')->getId() : null
206 +
            $block->getSetting('categoryId') instanceof CategoryInterface
207 +
                ? $block->getSetting('categoryId')->getId()
208 +
                : null
206 209
        );
207 210
    }
208 211
}
Files Complexity Coverage
src 348 55.37%
Project Totals (36 files) 348 55.37%
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading