Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
LinkTargetTrait
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 8
240
0.00% covered (danger)
0.00%
0 / 1
 inNamespace
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 hasFragment
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 removeFragmentTarget
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isExternal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isSameLinkAs
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
 getNamespaceName
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 __toString
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2declare( strict_types = 1 );
3
4/**
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Addshore
22 */
23namespace Wikimedia\Parsoid\Core;
24
25/**
26 * Useful helpers for LinkTarget implementations.
27 *
28 * @see LinkTarget
29 */
30trait LinkTargetTrait {
31
32    /**
33     * Convenience function to check if the target is in a given namespace.
34     *
35     * @param int $ns
36     * @return bool
37     */
38    public function inNamespace( int $ns ): bool {
39        '@phan-var LinkTarget $this';
40        // Core may someday switch to an enumerated type, but this is
41        // safe for now.
42        return $this->getNamespace() === $ns;
43    }
44
45    /**
46     * Whether the link target has a fragment.
47     *
48     * @return bool
49     */
50    public function hasFragment(): bool {
51        '@phan-var LinkTarget $this';
52        return $this->getFragment() !== '';
53    }
54
55    /**
56     * Create a new LinkTarget with no fragment on the same page.
57     *
58     * It is expected that the same type of object will be returned, but the
59     * only requirement is that it is a LinkTarget.
60     *
61     * @return LinkTarget
62     */
63    public function removeFragmentTarget(): LinkTarget {
64        // We're trying to deprecate this calling pattern (T19006)
65        // but it will work for transition purposes.
66        '@phan-var LinkTarget $this';
67        return $this->createFragmentTarget( '' );
68    }
69
70    /**
71     * Get the main part of the link target, in text form.
72     *
73     * The main part is the link target without namespace prefix or hash fragment.
74     * The text form is used for display purposes.
75     *
76     * This is computed from the DB key by replacing any underscores with spaces.
77     *
78     * @note To get a title string that includes the namespace and/or fragment,
79     *       use a TitleFormatter.
80     *
81     * @return string
82     */
83    public function getText(): string {
84        '@phan-var LinkTarget $this';
85        return strtr( $this->getDBKey(), '_', ' ' );
86    }
87
88    /**
89     * Whether this LinkTarget has an interwiki component.
90     *
91     * @return bool
92     */
93    public function isExternal(): bool {
94        '@phan-var LinkTarget $this';
95        return $this->getInterwiki() !== '';
96    }
97
98    /**
99     * Check whether the given LinkTarget refers to the same target as this LinkTarget.
100     *
101     * Two link targets are considered the same if they have the same interwiki prefix,
102     * are in the same namespace, have the same main part, and the same fragment.
103     *
104     * @param LinkTarget $other
105     * @return bool
106     */
107    public function isSameLinkAs( LinkTarget $other ): bool {
108        '@phan-var LinkTarget $this';
109        // NOTE: keep in sync with Title::isSameLinkAs()!
110        // NOTE: keep in sync with TitleValue::isSameLinkAs()!
111        // NOTE: === is needed for number-like titles
112        return ( $other->getInterwiki() === $this->getInterwiki() )
113            && ( $other->getDBkey() === $this->getDBkey() )
114            && ( $other->getNamespace() === $this->getNamespace() )
115            && ( $other->getFragment() === $this->getFragment() );
116    }
117
118    /**
119     * Return an informative human-readable representation of the link target
120     * namespace, for use in logging and debugging.
121     *
122     * @return string
123     */
124    public function getNamespaceName(): string {
125        '@phan-var LinkTarget $this';
126        if ( $this->getNamespace() === 0 ) {
127            return '';  // 0 is NS_MAIN
128        }
129        // A nicer version of this would convert the namespace to a
130        // human-readable string, but that's outside the bounds of the
131        // limited LinkTarget interface.  As a fallback, just use a
132        // numeric namespace.  Implementations can override this.
133        return '<' . strval( $this->getNamespace() ) . '>';
134    }
135
136    /**
137     * Return an informative human-readable representation of the link target,
138     * for use in logging and debugging.
139     *
140     * @return string
141     */
142    public function __toString(): string {
143        '@phan-var LinkTarget $this';
144        $result = '';
145        if ( $this->isExternal() ) {
146            $result .= $this->getInterwiki() . ':';
147        }
148        if ( $this->getNamespace() !== 0 ) { // 0 is NS_MAIN
149            '@phan-var LinkTargetTrait $this';
150            $result .= $this->getNamespaceName() . ':';
151        }
152        $result .= $this->getText();
153        if ( $this->hasFragment() ) {
154            $result .= '#' . $this->getFragment();
155        }
156        return $result;
157    }
158
159}