-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add preliminary support for PHP 5.4 #43
Open
LinusU
wants to merge
3
commits into
carthage-software:main
Choose a base branch
from
LinusU:lu-php54
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
71 changes: 71 additions & 0 deletions
71
crates/linter/src/plugin/compatibility/rules/php55/finally_feature.rs
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,71 @@ | ||
use indoc::indoc; | ||
|
||
use mago_ast::ast::*; | ||
use mago_php_version::PHPVersion; | ||
use mago_reporting::*; | ||
use mago_span::*; | ||
use mago_walker::Walker; | ||
|
||
use crate::context::LintContext; | ||
use crate::definition::RuleDefinition; | ||
use crate::definition::RuleUsageExample; | ||
use crate::rule::Rule; | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
pub struct FinallyFeatureRule; | ||
|
||
impl Rule for FinallyFeatureRule { | ||
fn get_definition(&self) -> RuleDefinition { | ||
RuleDefinition::enabled("Finally Feature", Level::Error) | ||
.with_maximum_supported_php_version(PHPVersion::PHP54) | ||
.with_description(indoc! {" | ||
Flags any usage of the `finally` keyword, which was introduced in PHP 5.5. | ||
|
||
In environments running older versions of PHP, you can use a `try`/`catch` block without a `finally` block instead. | ||
"}) | ||
.with_example(RuleUsageExample::valid( | ||
"Using a `try`/`catch` block without a `finally` block", | ||
indoc! {r#" | ||
<?php | ||
|
||
try { | ||
// Code that might throw an exception. | ||
} catch (Exception $e) { | ||
// Handle the exception. | ||
} | ||
"#}, | ||
)) | ||
.with_example(RuleUsageExample::invalid( | ||
"Using a `try`/`catch` block with a `finally` block", | ||
indoc! {r#" | ||
<?php | ||
|
||
try { | ||
// Code that might throw an exception. | ||
} catch (Exception $e) { | ||
// Handle the exception. | ||
} finally { | ||
// Code that should always run. | ||
} | ||
"#}, | ||
)) | ||
} | ||
} | ||
|
||
impl<'a> Walker<LintContext<'a>> for FinallyFeatureRule { | ||
fn walk_in_try<'ast>(&self, r#try: &'ast Try, context: &mut LintContext<'a>) { | ||
let Some(finally) = r#try.finally_clause.as_ref() else { | ||
return; | ||
}; | ||
|
||
let issue = Issue::new( | ||
context.level(), | ||
"The `finally` block is only available in PHP 5.5 and later.", | ||
) | ||
.with_annotation( | ||
Annotation::primary(finally.span()).with_message("Finally block used here."), | ||
); | ||
|
||
context.report(issue); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
crates/linter/src/plugin/compatibility/rules/php56/variadic_functions_feature.rs
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,73 @@ | ||||||
use indoc::indoc; | ||||||
|
||||||
use mago_ast::ast::*; | ||||||
use mago_php_version::PHPVersion; | ||||||
use mago_reporting::*; | ||||||
use mago_span::*; | ||||||
use mago_walker::Walker; | ||||||
|
||||||
use crate::context::LintContext; | ||||||
use crate::definition::RuleDefinition; | ||||||
use crate::definition::RuleUsageExample; | ||||||
use crate::rule::Rule; | ||||||
|
||||||
#[derive(Clone, Copy, Debug)] | ||||||
pub struct VariadicFunctionsFeatureRule; | ||||||
|
||||||
impl Rule for VariadicFunctionsFeatureRule { | ||||||
fn get_definition(&self) -> RuleDefinition { | ||||||
RuleDefinition::enabled("Variadic Functions Feature", Level::Error) | ||||||
.with_maximum_supported_php_version(PHPVersion::PHP55) | ||||||
.with_description(indoc! {" | ||||||
Flags any usage of variadic functions, which were introduced in PHP 5.6. | ||||||
|
||||||
In environments running older versions of PHP, you can use the `func_get_args()` function instead. | ||||||
"}) | ||||||
.with_example(RuleUsageExample::valid( | ||||||
"Using `func_get_args()` instead of variadic functions", | ||||||
indoc! {r#" | ||||||
<?php | ||||||
|
||||||
function sum() { | ||||||
$args = func_get_args(); | ||||||
return array_sum($args); | ||||||
} | ||||||
|
||||||
echo sum(1, 2, 3); | ||||||
"#}, | ||||||
)) | ||||||
.with_example(RuleUsageExample::invalid( | ||||||
"Using variadic functions", | ||||||
indoc! {r#" | ||||||
<?php | ||||||
|
||||||
function sum(...$args) { | ||||||
return array_sum($args); | ||||||
} | ||||||
|
||||||
echo sum(1, 2, 3); | ||||||
"#}, | ||||||
)) | ||||||
} | ||||||
} | ||||||
|
||||||
impl<'a> Walker<LintContext<'a>> for VariadicFunctionsFeatureRule { | ||||||
fn walk_in_function<'ast>(&self, function: &'ast Function, context: &mut LintContext<'a>) { | ||||||
for param in function.parameters.parameters.iter() { | ||||||
if param.ellipsis.is_none() { | ||||||
continue; | ||||||
} | ||||||
|
||||||
let issue = Issue::new( | ||||||
context.level(), | ||||||
"Variadic functions are only available in PHP 5.6 and later.", | ||||||
) | ||||||
.with_annotation( | ||||||
Annotation::primary(param.span()).with_message("Variadic parameter used here."), | ||||||
) | ||||||
.with_note("Use `func_get_args()` if you need compatibility with older PHP versions."); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
notes are used to provide more information, help are used to provide a solution, or a workaround. |
||||||
|
||||||
context.report(issue); | ||||||
} | ||||||
} | ||||||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this has became exlusive now.