Completed
Push — master ( c18f14...017364 )
by mw
292:38 queued 257:37
created

src/MediaWiki/Specials/Admin/IdActionHandler.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SMW\MediaWiki\Specials\Admin;
4
5
use SMW\ApplicationFactory;
6
use SMW\MediaWiki\Renderer\HtmlFormRenderer;
7
use SMW\MediaWiki\ManualEntryLogger;
8
use SMW\MediaWiki\Database;
9
use SMW\Message;
10
use SMW\Store;
11
use Html;
12
use WebRequest;
13
use User;
14
15
/**
16
 * @license GNU GPL v2+
17
 * @since   2.5
18
 *
19
 * @author mwjames
20
 */
21
class IdActionHandler {
22
23
	/**
24
	 * @var Store
25
	 */
26
	private $store;
27
28
	/**
29
	 * @var HtmlFormRenderer
30
	 */
31
	private $htmlFormRenderer;
32
33
	/**
34
	 * @var OutputFormatter
35
	 */
36
	private $outputFormatter;
37
38
	/**
39
	 * @var integer
40
	 */
41
	private $enabledFeatures = 0;
42
43
	/**
44
	 * @since 2.5
45
	 *
46
	 * @param Store $store
47
	 * @param HtmlFormRenderer $htmlFormRenderer
48
	 * @param OutputFormatter $outputFormatter
49
	 */
50 3
	public function __construct( Store $store, HtmlFormRenderer $htmlFormRenderer, OutputFormatter $outputFormatter ) {
51 3
		$this->store = $store;
52 3
		$this->htmlFormRenderer = $htmlFormRenderer;
53 3
		$this->outputFormatter = $outputFormatter;
54 3
	}
55
56
	/**
57
	 * @since 2.5
58
	 *
59
	 * @param integer $feature
60
	 *
61
	 * @return boolean
62
	 */
63 2
	public function isEnabledFeature( $feature ) {
64 2
		return ( $this->enabledFeatures & $feature ) != 0;
65
	}
66
67
	/**
68
	 * @since 2.5
69
	 *
70
	 * @param integer $enabledFeatures
71
	 */
72 1
	public function setEnabledFeatures( $enabledFeatures ) {
73 1
		$this->enabledFeatures = $enabledFeatures;
74 1
	}
75
76
	/**
77
	 * @since 2.5
78
	 *
79
	 * @param WebRequest $webRequest
80
	 * @param User|null $user
81
	 */
82 2
	public function performActionWith( WebRequest $webRequest, User $user = null ) {
83
84 2
		$this->outputFormatter->setPageTitle( $this->getMessage( 'smw-admin-supplementary-idlookup-title' ) );
85 2
		$this->outputFormatter->addParentLink();
86
87 2
		$id = (int)$webRequest->getText( 'id' );
88
89 2
		if ( $this->isEnabledFeature( SMW_ADM_DISPOSAL ) && $id > 0 && $webRequest->getText( 'dispose' ) === 'yes' ) {
90 1
			$this->doDispose( $id, $user );
91
		}
92
93 2
		$this->outputFormatter->addHtml( $this->getForm( $webRequest, $id ) );
94 2
	}
95
96
	/**
97
	 * @param integer $id
98
	 * @param User|null $use
0 ignored issues
show
There is no parameter named $use. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
99
	 */
100 1
	private function doDispose( $id, $user = null ) {
101
102 1
		$entityIdDisposerJob = ApplicationFactory::getInstance()->newJobFactory()->newEntityIdDisposerJob(
103 1
			\Title::newFromText( __METHOD__ )
104
		);
105
106 1
		$entityIdDisposerJob->dispose( $id );
107
108 1
		$manualEntryLogger = ApplicationFactory::getInstance()->create( 'ManualEntryLogger' );
109 1
		$manualEntryLogger->registerLoggableEventType( 'admin' );
110 1
		$manualEntryLogger->log( 'admin', $user, 'Special:SMWAdmin', 'Forced removal of ID '. $id );
111 1
	}
112
113 2
	private function getForm( $webRequest, $id ) {
114
115 2
		$message = $this->getIdInfoAsJson( $webRequest, $id );
116
117 2
		if ( $id < 1 ) {
118 1
			$id = null;
119
		}
120
121 2
		$html = $this->htmlFormRenderer
122 2
			->setName( 'idlookup' )
123 2
			->setMethod( 'get' )
124 2
			->addHiddenField( 'action', 'idlookup' )
125 2
			->addHiddenField( 'id', $id )
126 2
			->addParagraph( $this->getMessage( 'smw-admin-idlookup-docu' ) )
127 2
			->addInputField(
128 2
				$this->getMessage( 'smw-admin-objectid' ),
129 2
				'id',
130
				$id
131
			)
132 2
			->addNonBreakingSpace()
133 2
			->addSubmitButton( $this->getMessage( 'allpagessubmit' ) )
134 2
			->addParagraph( $message )
135 2
			->getForm();
136
137 2
		$html .= Html::element( 'p', array(), '' );
138
139 2
		if ( $id > 0 && $webRequest->getText( 'dispose' ) == 'yes' ) {
140
			$message = $this->getMessage( array ('smw-admin-iddispose-done', $id ) );
0 ignored issues
show
$message is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
141
			$id = null;
142
		}
143
144 2
		if ( !$this->isEnabledFeature( SMW_ADM_DISPOSAL ) ) {
145 1
			return $html;
146
		}
147
148 1
		$html .= $this->htmlFormRenderer
149 1
			->setName( 'iddispose' )
150 1
			->setMethod( 'get' )
151 1
			->addHiddenField( 'action', 'idlookup' )
152 1
			->addHiddenField( 'id', $id )
153 1
			->addHeader( 'h2', $this->getMessage( 'smw-admin-iddispose-title' ) )
154 1
			->addParagraph( $this->getMessage( 'smw-admin-iddispose-docu', Message::PARSE ) )
155 1
			->addInputField(
156 1
				$this->getMessage( 'smw-admin-objectid' ),
157 1
				'id',
158
				$id,
159 1
				null,
160 1
				20,
161 1
				'',
162 1
				true
163
			)
164 1
			->addNonBreakingSpace()
165 1
			->addSubmitButton( $this->getMessage( 'allpagessubmit' ) )
166 1
			->addCheckbox(
167 1
				$this->getMessage( 'smw_smwadmin_datarefreshstopconfirm', Message::ESCAPED ),
168 1
				'dispose',
169 1
				'yes'
170
			)
171 1
			->getForm();
172
173 1
		return $html . Html::element( 'p', array(), '' );
174
	}
175
176 2
	private function getIdInfoAsJson( $webRequest, $id ) {
177
178 2
		if ( $id < 1 || $webRequest->getText( 'action' ) !== 'idlookup' ) {
179 1
			return '';
180
		}
181
182 1
		$references = false;
183 1
		$row = $this->store->getConnection( 'mw.db' )->selectRow(
184 1
				\SMWSql3SmwIds::TABLE_NAME,
185
				array(
186 1
					'smw_title',
187
					'smw_namespace',
188
					'smw_iw',
189
					'smw_subobject',
190
					'smw_sortkey'
191
				),
192 1
				'smw_id=' . $id,
193 1
				__METHOD__
194
		);
195
196 1
		if ( $row !== false ) {
197
			$references = $this->store->getPropertyTableIdReferenceFinder()->searchAllTablesToFindAtLeastOneReferenceById(
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class SMW\Store as the method getPropertyTableIdReferenceFinder() does only exist in the following sub-classes of SMW\Store: SMWSQLStore3, SMWSparqlStore, SMW\SPARQLStore\SPARQLStore. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
198
				$id
199
			);
200
		}
201
202 1
		$output = '<pre>' . $this->outputFormatter->encodeAsJson( array( $id, $row ) ) . '</pre>';
203
204 1
		if ( $references ) {
205
			$output .= Html::element( 'p', array(), $this->getMessage( array( 'smw-admin-iddispose-references', $id, count( $references ) ) ) );
206
			$output .= '<pre>' . $this->outputFormatter->encodeAsJson( $references ) . '</pre>';
207
		}
208
209 1
		return $output;
210
	}
211
212 2
	private function getMessage( $key, $type = Message::TEXT ) {
213 2
		return Message::get( $key, $type, Message::USER_LANGUAGE );
214
	}
215
216
}
217