Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
HeadingItemTrait
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 getName
n/a
0 / 0
n/a
0 / 0
0
 getHeadingLevel
n/a
0 / 0
n/a
0 / 0
0
 isPlaceholderHeading
n/a
0 / 0
n/a
0 / 0
0
 jsonSerialize
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 isSubscribable
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Extension\DiscussionTools\ThreadItem;
4
5trait HeadingItemTrait {
6    // phpcs:disable Squiz.WhiteSpace, MediaWiki.Commenting
7    // Required ThreadItem methods (listed for Phan)
8    abstract public function getName(): string;
9    // Required HeadingItem methods (listed for Phan)
10    abstract public function getHeadingLevel(): int;
11    abstract public function isPlaceholderHeading(): bool;
12    // phpcs:enable
13
14    /**
15     * @inheritDoc
16     * @suppress PhanTraitParentReference
17     */
18    public function jsonSerialize( bool $deep = false, ?callable $callback = null ): array {
19        return array_merge( [
20            'headingLevel' => $this->isPlaceholderHeading() ? null : $this->getHeadingLevel(),
21            // Used for topic subscriptions. Not added to CommentItem's yet as there is
22            // no use case for it.
23            'name' => $this->getName(),
24        ], parent::jsonSerialize( $deep, $callback ) );
25    }
26
27    /**
28     * Check whether this heading can be used for topic subscriptions.
29     *
30     * @return bool
31     */
32    public function isSubscribable(): bool {
33        return (
34            // Placeholder headings have nothing to attach the button to.
35            !$this->isPlaceholderHeading() &&
36            // We only allow subscribing to level 2 headings, because the user interface for sub-headings
37            // would be difficult to present.
38            $this->getHeadingLevel() === 2 &&
39            // Check if the name corresponds to a section that contain no comments (only sub-sections).
40            // They can't be distinguished from each other, so disallow subscribing.
41            $this->getName() !== 'h-'
42        );
43    }
44}