Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW: Add phpstan level 1 analysis #9873

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"sminnee/phpunit": "^5.7.29",
"sminnee/phpunit-mock-objects": "^3.4.9",
"silverstripe/versioned": "^1",
"squizlabs/php_codesniffer": "^3.5"
"squizlabs/php_codesniffer": "^3.5",
"phpstan/phpstan": "^0.12.71"
},
"provide": {
"psr/container-implementation": "1.0.0"
Expand Down
47 changes: 47 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
parameters:
level: 1
paths:
- src
excludePaths:
# Attempts to link the Parser from php-peg crash phpstan
- src/View/SSTemplateParseException.php
- src/View/SSTemplateParser.php
ignoreErrors:
-
message: '#Class SilverStripe\\Admin\\LeftAndMain not found.#'
path: src/Forms/GridField/GridFieldDetailForm_ItemRequest.php
-
message: '#Class SilverStripe\\CMS\\Model\\SiteTree not found#'
path: src/Core/ClassInfo.php
-
message: '#Class SilverStripe\\CMS\\Model\\SiteTree not found#'
path: src/ORM/Search/FulltextSearchable.php
-
message: '#Class SilverStripe\\CMS\\Model\\SiteTree not found#'
path: src/ORM/Connect/MySQLDatabase.php
-
message: '#Class Page not found#'
path: src/Security/Security.php
-
message: '#Constructor of class .* has an unused parameter \$form#'
path: src/Forms/ConfirmedPasswordField.php
-
message: '#Constructor of class .* has an unused parameter \$extraFields.#'
path: src/ORM/ManyManyThroughList.php
-
message: '#Constructor of class .* has an unused parameter \$database.#'
path: src/ORM/Connect/MySQLQuery.php
-
message: '#Constructor of class .* has an unused parameter \$validator.#'
path: src/Security/LogoutForm.php
-
message: '#Class Parser was not found while trying to analyse it - discovering symbols is probably not configured properly.#'
path: src/View/SSTemplateParser.php
-
message: '#Class Parser was not found while trying to analyse it - discovering symbols is probably not configured properly.#'
path: src/View/SSTemplateParseException.php


scanDirectories:
- thirdparty/simpletest
- thirdparty/difflib
2 changes: 0 additions & 2 deletions src/Control/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,6 @@ public function getViewer($action)
$templates = $this->templates[$action];
} elseif (isset($this->templates['index']) && $this->templates['index']) {
$templates = $this->templates['index'];
} elseif ($this->template) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable isn't defined. In theory people could define it in a subclass, but it's far from being a published API. This code goes back to the project's inception and I think it's an oversight.

In Security.php, private static $template is defined, which does not populate this variable - it's used as a config var. Notably, this code on PHP 7.4 failed to print HELLO

<?php

class A {
  function foo() {
    return $this->template;
  }
}

class B extends A {
  static $template = 'HELLO';
}

echo (new B())->foo();

In my view, removing this isn't a breaking change, but it's moderately controversial. As an alternative I would add a /** @phpstan-ignore-next-line */ flag; attempting to define template breaks other things.

$templates = $this->template;
} else {
// Build templates based on class hierarchy
$actionTemplates = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Control/HTTPStreamResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function getSavedBody()
public function getBody()
{
$body = $this->getSavedBody();
if (isset($body)) {
if ($body !== null) {
return $body;
}

Expand Down
10 changes: 7 additions & 3 deletions src/Control/SimpleResourceURLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public function urlForResource($relativePath)
// Apply nonce
// Don't add nonce to directories
if ($this->nonceStyle && $exists && is_file($absolutePath)) {
$method = null;

switch ($this->nonceStyle) {
case 'mtime':
$method = 'filemtime';
Expand All @@ -114,10 +116,12 @@ public function urlForResource($relativePath)
break;
}

if ($query) {
$query .= '&';
if ($method && function_exists($method)) {
if ($query) {
$query .= '&';
}
$query .= "m=" . call_user_func($method, $absolutePath);
}
$query .= "m=" . call_user_func($method, $absolutePath);
}

// Add back querystring
Expand Down
5 changes: 3 additions & 2 deletions src/Core/Config/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ class ConfigLoader
*/
public static function inst()
{
return self::$instance ? self::$instance : self::$instance = new static();
return self::$instance ? self::$instance : self::$instance = new self();
}


/**
* Returns the currently active class manifest instance that is used for
* loading classes.
Expand Down Expand Up @@ -97,7 +98,7 @@ public function nest()
$manifest = clone $this->getManifest();

// Create new blank loader with new stack (top level nesting)
$newLoader = new static;
$newLoader = new self();
$newLoader->pushManifest($manifest);

// Activate new loader
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static function increaseMemoryLimitTo($memoryLimit = -1)
*/
static function setMemoryLimitMax($memoryLimit)
{
if (isset($memoryLimit) && !is_numeric($memoryLimit)) {
if ($memoryLimit !== null && !is_numeric($memoryLimit)) {
$memoryLimit = Convert::memstring2bytes($memoryLimit);
}
static::$memoryLimitMax = $memoryLimit;
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Injector/InjectorLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class InjectorLoader
*/
public static function inst()
{
return self::$instance ? self::$instance : self::$instance = new static();
return self::$instance ? self::$instance : self::$instance = new self();
}

/**
Expand Down Expand Up @@ -96,7 +96,7 @@ public function nest()
$manifest = clone $this->getManifest();

// Create new blank loader with new stack (top level nesting)
$newLoader = new static;
$newLoader = new self();
$newLoader->pushManifest($manifest);

// Activate new loader
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Manifest/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ClassLoader
*/
public static function inst()
{
return self::$instance ? self::$instance : self::$instance = new static();
return self::$instance ? self::$instance : self::$instance = new self();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Manifest/ClassManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public function regenerate($includeTests)
'name_regex' => '/^[^_].*\\.php$/',
'ignore_files' => ['index.php', 'cli-script.php'],
'ignore_tests' => !$includeTests,
'file_callback' => function ($basename, $pathname, $depth) use ($includeTests, $finder) {
'file_callback' => function ($basename, $pathname, $depth) use ($includeTests) {
$this->handleFile($basename, $pathname, $includeTests);
},
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Manifest/ModuleLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ModuleLoader
*/
public static function inst()
{
return self::$instance ? self::$instance : self::$instance = new static();
return self::$instance ? self::$instance : self::$instance = new self();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Core/Manifest/VersionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ protected function getComposerLock($cache = true)
$jsonData = file_get_contents($composerLockPath);

if ($cache) {
$cache = Injector::inst()->get(CacheInterface::class . '.VersionProvider_composerlock');
$cacheStore = Injector::inst()->get(CacheInterface::class . '.VersionProvider_composerlock');
$cacheKey = md5($jsonData);
if ($versions = $cache->get($cacheKey)) {
if ($versions = $cacheStore->get($cacheKey)) {
$lockData = json_decode($versions, true);
}
}
Expand All @@ -157,7 +157,7 @@ protected function getComposerLock($cache = true)
$lockData = json_decode($jsonData, true);

if ($cache) {
$cache->set($cacheKey, $jsonData);
$cacheStore->set($cacheKey, $jsonData);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Core/Startup/AbstractConfirmationToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ abstract class AbstractConfirmationToken
*/
protected $token = null;

/**
* Create a token with the given name for the given request
*/
abstract function __construct($parameterName, HTTPRequest $request);

/**
* Given a list of token names, suppress all tokens that have not been validated, and
* return the non-validated token with the highest priority
Expand Down
5 changes: 4 additions & 1 deletion src/Dev/CsvBulkLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ protected function processAll($filepath, $preview = false)
return $row;
};

$headerMap = null;
if ($this->columnMap) {
$headerMap = $this->getNormalisedColumnMap();

Expand Down Expand Up @@ -125,12 +126,14 @@ protected function processAll($filepath, $preview = false)
$csvReader->setHeaderOffset(0);
$rows = new MapIterator($csvReader->getRecords(), $remapper);
}
} elseif ($this->columnMap) {
} elseif ($headerMap) {
if (method_exists($csvReader, 'fetchAssoc')) {
$rows = $csvReader->fetchAssoc($headerMap, $remapper);
} else {
$rows = new MapIterator($csvReader->getRecords($headerMap), $remapper);
}
} else {
$rows = [];
}

foreach ($rows as $row) {
Expand Down
37 changes: 1 addition & 36 deletions src/Dev/DebugView.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,7 @@ class DebugView
private static $columns = 100;

protected static $error_types = [
0 => [
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These values aren't being used anywhere, only the errno-mapped ones are. And 2 conflicted with E_USER_WARNING

'title' => 'Emergency',
'class' => 'error'
],
1 => [
'title' => 'Alert',
'class' => 'error'
],
2 => [
'title' => 'Critical',
'class' => 'error'
],
3 => [
'title' => 'Error',
'class' => 'error'
],
4 => [
'title' => 'Warning',
'class' => 'warning'
],
5 => [
'title' => 'Notice',
'class' => 'notice'
],
6 => [
'title' => 'Information',
'class' => 'info'
],
7=> [
'title' => 'SilverStripe\\Dev\\Debug',
'class' => 'debug'
],

E_USER_ERROR => [
'title' => 'User Error',
'class' => 'error'
Expand All @@ -86,10 +55,6 @@ class DebugView
'title' => 'User Deprecated',
'class' => 'notice'
],
E_CORE_ERROR => [
'title' => 'Core Error',
'class' => 'error'
],
E_WARNING => [
'title' => 'Warning',
'class' => 'warning'
Expand Down
3 changes: 2 additions & 1 deletion src/Dev/Install/DatabaseAdapterRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace SilverStripe\Dev\Install;

use Exception;
use InvalidArgumentException;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Core\Flushable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Core\Flushable;

/**
* This class keeps track of the available database adapters
Expand Down
5 changes: 5 additions & 0 deletions src/Forms/FieldList.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class FieldList extends ArrayList
*/
protected $sequentialSaveableSet;

/**
* @deprecated
*/
public $form;

/**
* If this fieldlist is owned by a parent field (e.g. CompositeField)
* this is the parent field.
Expand Down
4 changes: 2 additions & 2 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,13 @@ public function restoreFormState()
{
// Restore messages
$result = $this->getSessionValidationResult();
if (isset($result)) {
if ($result !== null) {
$this->loadMessagesFrom($result);
}

// load data in from previous submission upon error
$data = $this->getSessionData();
if (isset($data)) {
if ($data !== null) {
$this->loadDataFrom($data, self::MERGE_AS_INTERNAL_VALUE);
}
return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ public function extraClass()
//
// CSS class needs to be different from the one rendered through {@link FieldHolder()}.
if ($this->getMessage()) {
$classes[] .= 'holder-' . $this->getMessageType();
$classes[] = 'holder-' . $this->getMessageType();
}

return implode(' ', $classes);
Expand Down
5 changes: 5 additions & 0 deletions src/Forms/GridField/GridField.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ class GridField extends FormField
*/
protected $name = '';

/**
* @deprecated
*/
public $id;

/**
* A whitelist of readonly component classes allowed if performReadonlyTransform is called.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/GridField/GridFieldFilterHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public function getSearchFormSchema(GridField $gridField)
return new HTTPResponse(_t(__CLASS__ . '.SearchFormFaliure', 'No search form could be generated'), 400);
}

$parts = $gridField->getRequest()->getHeader(LeftAndMain::SCHEMA_HEADER);
$parts = $gridField->getRequest()->getHeader('X-Formschema-Request');
$schemaID = $gridField->getRequest()->getURL();
$data = FormSchema::singleton()
->getMultipartSchema($parts, $schemaID, $form);
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/NullableField.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function setName($name)
public function debug()
{
$result = sprintf(
'%s (%s: $s : <span style="color: red">%s</span>) = ',
'%s (%s: %s : <span style="color: red">%s</span>) = ',
static::class,
$this->name,
$this->title,
Expand Down
Loading