-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Universal/ConstructorDestructorReturn: add auto-fixer for return type
The "returns a value" warning cannot be auto-fixed as it should be looked at by a developer. However, the "return type found" error _can_ be auto-fixed, so let's do so.
- Loading branch information
Showing
2 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.inc.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
/* | ||
* OK. | ||
*/ | ||
|
||
// Global function, not a constructor/destructor. | ||
function __construct() : int { | ||
return 123; | ||
} | ||
|
||
function __destruct() : float { | ||
return 321; | ||
} | ||
|
||
// Methods which are not constructors can be disregarded. | ||
class NotAConstructor { | ||
public function __set($name, $value): void { | ||
// Do something. | ||
} | ||
|
||
public function Foo(): void { | ||
// Do something. | ||
} | ||
|
||
public function Bar() { | ||
return $this; | ||
} | ||
} | ||
|
||
// Constructor/Destructor without a return statement or return type. | ||
class NoReturn { | ||
protected function __construct() | ||
{ | ||
// Do something. | ||
} | ||
|
||
protected function __destruct() | ||
{ | ||
// Do something. | ||
} | ||
|
||
// Also applies to PHP4-style constructors/destructors. | ||
function NoReturn() | ||
{ | ||
// Do something. | ||
} | ||
|
||
function _NoReturn() | ||
{ | ||
// Do something. | ||
} | ||
} | ||
|
||
// Constructor/Destructor with return statement, but no value. | ||
$anon = new class extends ReturnNoValue { | ||
public function __construct() { | ||
if ($foo) { | ||
return ; | ||
} else { | ||
return /* comments are fine */ | ||
// Even when spread over multiple lines. | ||
; | ||
} | ||
|
||
return; | ||
} | ||
|
||
public function __destruct() { | ||
// Do something. | ||
return; | ||
} | ||
|
||
// Non-constructor/destructor method returning. | ||
public function returnsavalue() { | ||
return 'php4style'; | ||
} | ||
}; | ||
|
||
|
||
/* | ||
* Not OK. | ||
*/ | ||
class ReturnsAValue { | ||
public function __construct() { | ||
return $this; | ||
} | ||
|
||
public function __destruct() { | ||
return 'destructed'; | ||
} | ||
|
||
function returnsavalue() | ||
{ | ||
return 'php4style'; | ||
} | ||
} | ||
|
||
$anon = new class() extends ReturnsAValue { | ||
public function __Construct() | ||
{ | ||
return $this; | ||
} | ||
|
||
public function __deStRucT() { | ||
return 'destructed'; | ||
} | ||
}; | ||
|
||
/* | ||
* Return types are not allowed on constructor/destructor methods (fatal error). | ||
*/ | ||
|
||
trait AbstractConstructorDestructorReturnTypes { | ||
abstract public function __construct(); | ||
|
||
abstract public function __destruct(); | ||
} | ||
|
||
interface InterfaceMethodReturnTypes { | ||
public function __construct(); | ||
|
||
public function __destruct(); | ||
} |