Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/upstream/5'
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwan Mustafa Fikrat committed May 7, 2024
2 parents b48766e + aff905c commit 6a4601e
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 188 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@ on:
paths-ignore:
- .chglog/**
- .github/**
- "!.github/workflows/tests.yml"
- '!.github/workflows/tests.yml'
- .editorconfig
- .gitattributes
- .gitignore
- .htaccess
- "**.md"
- '**.md'
branches:
- master
- '5'
pull_request:
paths-ignore:
- .chglog/**
- .github/**
- "!.github/workflows/tests.yml"
- '!.github/workflows/tests.yml'
- .editorconfig
- .gitattributes
- .gitignore
- .htaccess
- "**.md"
- '**.md'
branches:
- master
pull_request_target:
types: [opened, synchronize, ready_for_review]
- '5'

jobs:
silverstripe-module:
name: 🧰 Silverstripe Module Testsuite
uses: syntro-opensource/workflows/.github/workflows/silverstripe-module.yml@master
uses: syntro-opensource/workflows/.github/workflows/silverstripe-module.yml@5
with:
phpunit: true
phpunit_config_file: tests/phpunit.xml
Expand Down
19 changes: 18 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]


<a name="5.0.0"></a>
## [5.0.0] - 2023-06-28
### 🍰 Added
- support for dot notations in DataObject Relations ([#14](https://github.com/syntro-opensource/silverstripe-phpstan/issues/14)) ([#21](https://github.com/syntro-opensource/silverstripe-phpstan/issues/21))
- information for people still using Silverstripe 4
- support for silverstripe 5 ([#19](https://github.com/syntro-opensource/silverstripe-phpstan/issues/19))
- Stubs for Page and PageController to ensure these symbols also exist for modules ([#18](https://github.com/syntro-opensource/silverstripe-phpstan/issues/18))
- standardized testsuites ([#11](https://github.com/syntro-opensource/silverstripe-phpstan/issues/11))

### 🐞 Fixed
- tests are actually executed for the 5 branch

### 🔧 Changed
- Uses v5 testsuite


<a name="1.0.0"></a>
## [1.0.0] - 2022-04-30
### 🍰 Added
Expand All @@ -31,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- github action tests ([#3](https://github.com/syntro-opensource/silverstripe-phpstan/issues/3))


[Unreleased]: https://github.com/syntro-opensource/silverstripe-phpstan/compare/1.0.0...HEAD
[Unreleased]: https://github.com/syntro-opensource/silverstripe-phpstan/compare/5.0.0...HEAD
[5.0.0]: https://github.com/syntro-opensource/silverstripe-phpstan/compare/1.0.0...5.0.0
[1.0.0]: https://github.com/syntro-opensource/silverstripe-phpstan/compare/0.0.0...1.0.0
[0.0.0]: https://github.com/syntro-opensource/silverstripe-phpstan/compare/fork...0.0.0
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ This PHPStan module is able to reason about extensions installed specific to you

## Composer Install

SilverStripe 4.X
SilverStripe 5.X
```
composer require --dev syntro/silverstripe-phpstan
composer require --dev syntro/silverstripe-phpstan ^5
```

> For Silverstripe 4, see [the corresponding branch](https://github.com/syntro-opensource/silverstripe-phpstan/tree/4).
## Requirements

* SilverStripe 4.3+
* SilverStripe 5.0+

## Documentation

Expand All @@ -42,7 +42,7 @@ composer require --dev syntro/silverstripe-phpstan
* PHPStan checks if properties are actually initialized, read and written. This
however does not apply to Silverstripes [configuration properties](https://docs.silverstripe.org/en/4/developer_guides/configuration/configuration/#configuration-properties).
In order to tell PHPStan which of your properties are configuration values, you have
to use the [`@config` docblock](https://docs.silverstripe.org/en/4/developer_guides/configuration/configuration/#configuration-properties).
to use the [`@config` docblock](https://docs.silverstripe.org/en/5/developer_guides/configuration/configuration/#configuration-properties).
* The type of the `owner` property can't be reasoned about for extensions. You must use `getOwner()`. Related Issues: [#1043](https://github.com/phpstan/phpstan/issues/1043) and [#1044](https://github.com/phpstan/phpstan/issues/1044)

## Credits
Expand Down
10 changes: 10 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
exit(1);
}

// Ensure that the `Page` and `PageController` classes exist, if they don't _(for example when testing a
// silverstripe module)_ ensure that the symbols are registered.
if (!class_exists(\Page::class)) {
require __DIR__ . '/stubs/Page.php';
}

if (!class_exists(\PageController::class)) {
require __DIR__ . '/stubs/PageController.php';
}

// Ensure global $_SERVER exists
global $_SERVER;
if (!$_SERVER) {
Expand Down
31 changes: 7 additions & 24 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,22 @@
}
],
"require": {
"php": "~7.1 || ~8.0",
"silverstripe/framework": "~4.3",
"silverstripe/vendor-plugin": "^1.0",
"php": "~8.0",
"silverstripe/framework": "~5.0",
"phpstan/phpstan": "^1.5",
"silverstripe/recipe-cms": "^4.11.0"
"silverstripe/versioned": "^2.0",
"silverstripe/cms": "^5.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.0",
"phpstan/phpstan-phpunit": "^1",
"phpunit/phpunit": "^9.4"
},
"scripts": {
"build": "sake dev/build",
"phpcs": "phpcs -n -l src/ src/Reflection/ src/Rule/ src/Type tests/ tests/Reflection/ tests/Rule/ tests/Type/",
"phpcbf": "phpcbf -n src/ src/Reflection/ src/Rule/ src/Type tests/ tests/Reflection/ tests/Rule/ tests/Type/",
"phpunit": "phpunit -c \"tests/phpunit.xml\" tests/",
"phpstan": "phpstan analyse src/ tests/ -c \"tests/phpstan.neon\" -a \"tests/bootstrap-phpstan.php\" --level 4 --memory-limit=1G",
"post-install-cmd": [
"php ./other/extract-phar.php"
]
"phpunit": "bash ../../../vendor/bin/phpunit -c \"tests/phpunit.xml\" tests/",
"phpstan": "bash ../../../vendor/bin/phpstan analyse src/ tests/ -c \"tests/phpstan.neon\" -a \"tests/bootstrap-phpstan.php\" --level 4"
},
"autoload": {
"psr-4": {
Expand All @@ -52,20 +48,7 @@
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
},
"project-files-installed": [
"app/.htaccess",
"app/_config.php",
"app/_config/mimevalidator.yml",
"app/_config/mysite.yml",
"app/src/Page.php",
"app/src/PageController.php"
],
"public-files-installed": [
".htaccess",
"index.php",
"web.config"
]
}
},
"config": {
"allow-plugins": {
Expand Down
6 changes: 0 additions & 6 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ parameters:
excludePaths:
- silverstripe-cache
services:
# This rule will throw an error if you `return false` from a RequestFilter::preRequest() method
# as in SilverStripe 3.X, this throws an uncaught exception in the site.
-
class: Syntro\SilverstripePHPStan\Rule\RequestFilterPreRequestRule
tags:
- phpstan.rules.rule
# This adds additional methods from SilverStripe extensions, as well as determining proper
# types when has_one() magic methods.
-
Expand Down
2 changes: 1 addition & 1 deletion src/ClassHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ClassHelper

// Controller
const ContentController = \SilverStripe\CMS\Controllers\ContentController::class;
const RequestFilter = \SilverStripe\Control\RequestFilter::class;
const HTTPMiddleware = \SilverStripe\Control\Middleware\HTTPMiddleware::class;

// FormField
const DBField = \SilverStripe\ORM\FieldType\DBField::class;
Expand Down
55 changes: 0 additions & 55 deletions src/Rule/RequestFilterPreRequestRule.php

This file was deleted.

8 changes: 8 additions & 0 deletions stubs/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace {

use SilverStripe\CMS\Model\SiteTree;

class Page extends SiteTree {}
}
8 changes: 8 additions & 0 deletions stubs/PageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace {

use SilverStripe\CMS\Controllers\ContentController;

class PageController extends ContentController {}
}
39 changes: 39 additions & 0 deletions tests/Reflection/MethodClassReflectionExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Syntro\SilverstripePHPStan\Tests\Reflection;

use MethodClassReflectionReturnTypesNamespace\Team;
use PHPStan\Testing\TypeInferenceTestCase;
use SilverStripe\Core\Config\Config;

class MethodClassReflectionExtensionTest extends TypeInferenceTestCase
{
/**
* @return iterable<mixed>
*/
public function dataFileAsserts(): iterable
{
// path to a file with actual asserts of expected types:
require_once(__DIR__ . '/data/method-class-reflection.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/method-class-reflection.php');
}

/**
* @dataProvider dataFileAsserts
*/
public function testFileAsserts(
string $assertType,
string $file,
...$args
): void {
$this->assertFileAsserts($assertType, $file, ...$args);
}

public static function getAdditionalConfigFiles(): array
{
// path to your project's phpstan.neon, or extension.neon in case of custom extension packages
return [
__DIR__ . '/../../phpstan.neon'
];
}
}
73 changes: 73 additions & 0 deletions tests/Reflection/data/method-class-reflection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
// @codingStandardsIgnoreStart
namespace MethodClassReflectionReturnTypesNamespace;

// SilverStripe
use SilverStripe\Core\Config\Config;
use SilverStripe\ORM\DataObject;
use Syntro\SilverstripePHPStan\ClassHelper;
use function PHPStan\Testing\assertType;

/**
* setting up relations in the Classes doesn't get picked up
* Need to add to the config manually
* @return void
*/
function initDbRelations() {
Config::modify()->merge(Team::class, 'has_many', [
'Players' => Player::class,
'WinningPlayers' => Player::class . '.WinningTeam'
]);

Config::modify()->merge(Player::class, 'has_one', [
'Team' => Team::class,
'WinningTeam' => Team::class . '.WinningPlayers'
]);

Config::modify()->merge(Coach::class, 'belongs_to', [
'Team' => Team::class,
'WinningTeam' => Team::class . '.WinningCoach',
]);
}

initDbRelations();

class Foo
{
public function doFoo(): void
{


$player = new Player;
// Standard has_one
assertType(Team::class, $player->Team());
// Has one with a custom relation name
assertType(Team::class, $player->WinningTeam());

$team = new Team();
// Standard has_one
assertType( sprintf('%s<%s>', ClassHelper::HasManyList, Player::class), $team->Players());
// Has one with a custom relation name
assertType( sprintf('%s<%s>', ClassHelper::HasManyList, Player::class), $team->WinningPlayers());

$coach = new Coach();
assertType(Team::class, $coach->Team());
// Has one with a custom relation name
assertType(Team::class, $coach->WinningTeam());
die();
}
}

class Team extends DataObject
{

}

class Player extends DataObject
{
}

class Coach extends DataObject
{
}
// @codingStandardsIgnoreEnd
Loading

0 comments on commit 6a4601e

Please sign in to comment.