Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[PHPUnitBridge] Fix deprecation type detection when trigger_deprecation is used #38013

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

Merged
merged 1 commit into from
Sep 2, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,18 @@ class Deprecation
*/
public function __construct($message, array $trace, $file)
{
if (isset($trace[2]['function']) && 'trigger_deprecation' === $trace[2]['function']) {
$file = $trace[2]['file'];
array_splice($trace, 1, 1);
}

$this->trace = $trace;
$this->message = $message;
$i = \count($trace);
while (1 < $i && $this->lineShouldBeSkipped($trace[--$i])) {
$i = \count($this->trace);
while (1 < $i && $this->lineShouldBeSkipped($this->trace[--$i])) {
// No-op
}
$line = $trace[$i];
$line = $this->trace[$i];
$this->triggeringFile = $file;
if (isset($line['object']) || isset($line['class'])) {
if (isset($line['class']) && 0 === strpos($line['class'], SymfonyTestsListenerFor::class)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Services;

use acme\lib\SomeService;
use foo\lib\SomeOtherService;

final class AppService
{
public function directDeprecationsTwoVendors()
{
$service1 = new SomeService();
$service1->deprecatedApi();

$service2 = new SomeOtherService();
$service2->deprecatedApi();
}

public function selfDeprecation(bool $useContracts = false)
{
$args = [__FUNCTION__, __FUNCTION__];
if ($useContracts) {
trigger_deprecation('App', '3.0', sprintf('%s is deprecated, use %s_new instead.', ...$args));
} else {
@trigger_error(sprintf('Since App 3.0: %s is deprecated, use %s_new instead.', ...$args), E_USER_DEPRECATED);
}
}

public function directDeprecation(bool $useContracts = false)
{
$service = new SomeService();
$service->deprecatedApi($useContracts);
}

public function indirectDeprecation(bool $useContracts = false)
{
$service = new SomeService();
$service->indirectDeprecatedApi($useContracts);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

namespace acme\lib;

use bar\lib\AnotherService;

class SomeService
{
public function deprecatedApi()
public function deprecatedApi(bool $useContracts = false)
{
$args = [__FUNCTION__, __FUNCTION__];
if ($useContracts) {
trigger_deprecation('acme/lib', '3.0', sprintf('%s is deprecated, use %s_new instead.', ...$args));
} else {
@trigger_error(sprintf('Since acme/lib 3.0: %s is deprecated, use %s_new instead.', ...$args), E_USER_DEPRECATED);
}
}

public function indirectDeprecatedApi(bool $useContracts = false)
{
@trigger_error(
__FUNCTION__.' is deprecated! You should stop relying on it!',
E_USER_DEPRECATED
);
(new AnotherService())->deprecatedApi($useContracts);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace bar\lib;

class AnotherService
{
public function deprecatedApi(bool $useContracts = false)
{
$args = [__FUNCTION__, __FUNCTION__];
if ($useContracts) {
trigger_deprecation('bar/lib', '3.0', sprintf('%s is deprecated, use %s_new instead.', ...$args));
} else {
@trigger_error(sprintf('Since bar/lib 3.0: %s is deprecated, use %s_new instead.', ...$args), E_USER_DEPRECATED);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,41 @@ public function getPrefixes()

public function getPrefixesPsr4()
{
return [];
return [
'App\\Services\\' => [__DIR__.'/../../fake_app/'],
'acme\\lib\\' => [__DIR__.'/../acme/lib/'],
'bar\\lib\\' => [__DIR__.'/../bar/lib/'],
];
}

public function loadClass($className)
{
foreach ($this->getPrefixesPsr4() as $prefix => $baseDirs) {
if (strpos($className, $prefix) !== 0) {
continue;
}

foreach ($baseDirs as $baseDir) {
$file = str_replace([$prefix, '\\'], [$baseDir, '/'], $className.'.php');
if (file_exists($file)) {
require $file;
}
}
}
}
}

class ComposerAutoloaderInitFake
{
private static $loader;

public static function getLoader()
{
return new ComposerLoaderFake();
if (null === self::$loader) {
self::$loader = new ComposerLoaderFake();
spl_autoload_register([self::$loader, 'loadClass']);
}

return self::$loader;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ require __DIR__.'/fake_vendor/acme/outdated-lib/outdated_file.php';
--EXPECTF--
Remaining indirect deprecation notices (1)

1x: deprecatedApi is deprecated! You should stop relying on it!
1x: Since acme/lib 3.0: deprecatedApi is deprecated, use deprecatedApi_new instead.
1x in SomeService::deprecatedApi from acme\lib
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Remaining direct deprecation notices (1)

Remaining indirect deprecation notices (1)

1x: deprecatedApi is deprecated! You should stop relying on it!
1x: Since acme/lib 3.0: deprecatedApi is deprecated, use deprecatedApi_new instead.
1x in SomeService::deprecatedApi from acme\lib

Legacy deprecation notices (2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ require __DIR__.'/fake_vendor/acme/outdated-lib/outdated_file.php';
--EXPECTF--
Remaining indirect deprecation notices (1)

1x: deprecatedApi is deprecated! You should stop relying on it!
1x: Since acme/lib 3.0: deprecatedApi is deprecated, use deprecatedApi_new instead.
1x in SomeService::deprecatedApi from acme\lib
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
--TEST--
Test deprecation types with trigger_deprecation
--FILE--
<?php

$k = 'SYMFONY_DEPRECATIONS_HELPER';
putenv($k.'='.$_SERVER[$k] = $_ENV[$k] = 'max[self]=0');
putenv('ANSICON');
putenv('ConEmuANSI');
putenv('TERM');

$vendor = __DIR__;
while (!file_exists($vendor.'/vendor')) {
$vendor = dirname($vendor);
}
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
require PHPUNIT_COMPOSER_INSTALL;
require_once __DIR__.'/../../bootstrap.php';

eval(<<<'EOPHP'
namespace PHPUnit\Util;

class Test
{
public static function getGroups()
{
return array();
}
}
EOPHP
);

require __DIR__.'/fake_vendor/autoload.php';

(new \App\Services\AppService())->selfDeprecation(true);
(new \App\Services\AppService())->directDeprecation(true);
(new \App\Services\AppService())->indirectDeprecation(true);
trigger_deprecation('foo/bar', '2.0', 'func is deprecated, use new instead.');
?>
--EXPECTF--
Remaining self deprecation notices (1)

1x: Since App 3.0: selfDeprecation is deprecated, use selfDeprecation_new instead.
1x in AppService::selfDeprecation from App\Services

Remaining direct deprecation notices (1)

1x: Since acme/lib 3.0: deprecatedApi is deprecated, use deprecatedApi_new instead.
1x in AppService::directDeprecation from App\Services

Remaining indirect deprecation notices (1)

1x: Since bar/lib 3.0: deprecatedApi is deprecated, use deprecatedApi_new instead.
1x in AppService::indirectDeprecation from App\Services

Other deprecation notices (1)

1x: Since foo/bar 2.0: func is deprecated, use new instead.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
--TEST--
Test deprecation types with trigger_error
--FILE--
<?php

$k = 'SYMFONY_DEPRECATIONS_HELPER';
putenv($k.'='.$_SERVER[$k] = $_ENV[$k] = 'max[self]=0');
putenv('ANSICON');
putenv('ConEmuANSI');
putenv('TERM');

$vendor = __DIR__;
while (!file_exists($vendor.'/vendor')) {
$vendor = dirname($vendor);
}
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
require PHPUNIT_COMPOSER_INSTALL;
require_once __DIR__.'/../../bootstrap.php';

eval(<<<'EOPHP'
namespace PHPUnit\Util;

class Test
{
public static function getGroups()
{
return array();
}
}
EOPHP
);

require __DIR__.'/fake_vendor/autoload.php';

(new \App\Services\AppService())->selfDeprecation();
(new \App\Services\AppService())->directDeprecation();
(new \App\Services\AppService())->indirectDeprecation();
@trigger_error('Since foo/bar 2.0: func is deprecated, use new instead.', E_USER_DEPRECATED);
?>
--EXPECTF--
Remaining self deprecation notices (1)

1x: Since App 3.0: selfDeprecation is deprecated, use selfDeprecation_new instead.
1x in AppService::selfDeprecation from App\Services

Remaining direct deprecation notices (1)

1x: Since acme/lib 3.0: deprecatedApi is deprecated, use deprecatedApi_new instead.
1x in AppService::directDeprecation from App\Services

Remaining indirect deprecation notices (1)

1x: Since bar/lib 3.0: deprecatedApi is deprecated, use deprecatedApi_new instead.
1x in AppService::indirectDeprecation from App\Services

Other deprecation notices (1)

1x: Since foo/bar 2.0: func is deprecated, use new instead.
3 changes: 3 additions & 0 deletions 3 src/Symfony/Bridge/PhpUnit/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@
"name": "phpunit/phpunit",
"url": "https://github.com/sebastianbergmann/phpunit"
}
},
"require-dev": {
"symfony/deprecation-contracts": "^2.1"
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.