Skip to content

[Yaml] Remove internal arguments from the api #21350

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

Closed
wants to merge 2 commits 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
7 changes: 7 additions & 0 deletions UPGRADE-3.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,10 @@ Workflow

* Deprecated class name support in `WorkflowRegistry::add()` as second parameter.
Wrap the class name in an instance of ClassInstanceSupportStrategy instead.

Yaml
----

* The constructor arguments `$offset`, `$totalNumberOfLines` and
`$skippedLineNumbers` of the `Parser` class are deprecated and will be
removed in 4.0
3 changes: 3 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ Yaml

* Duplicate mapping keys lead to a `ParseException`.

* The constructor arguments `$offset`, `$totalNumberOfLines` and
`$skippedLineNumbers` of the `Parser` class were removed.

Ldap
----

Expand Down
29 changes: 17 additions & 12 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ class Parser
private $skippedLineNumbers = array();
private $locallySkippedLineNumbers = array();

/**
* Constructor.
*
* @param int $offset The offset of YAML document (used for line numbers in error messages)
* @param int|null $totalNumberOfLines The overall number of lines being parsed
* @param int[] $skippedLineNumbers Number of comment lines that have been skipped by the parser
*/
public function __construct($offset = 0, $totalNumberOfLines = null, array $skippedLineNumbers = array())
public function __construct()
{
$this->offset = $offset;
$this->totalNumberOfLines = $totalNumberOfLines;
$this->skippedLineNumbers = $skippedLineNumbers;
if (func_num_args() > 0) {
Copy link
Contributor

@Taluu Taluu Jan 20, 2017

Choose a reason for hiding this comment

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

instead of having to do a if func > 0, if func_args > 1, ..., you can leave the optionnal parameters in the constructor (just undocument those) and just check if func_num_args > 0 to trigger the deprecation :

<?php
$f = function ($a = null, $b = null, array $c = array())
{
    var_dump(func_num_args());

    if (func_num_args() > 0) {
      trigger();
    }

   // ...
}

$f(); // output 0, no deprecation
$f(1); // output 1, deprecation
// and so on

It's cleaner IMO (and less conditionnals too)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But then they'll be automatically inserted by IDEs. Imo it is better to remove them to ensure people won't use deprecated arguments and only know it by executing the code.

@trigger_error(sprintf('The constructor arguments $offset, $totalNumberOfLines, $skippedLineNumbers of %s are deprecated and will be removed in 4.0', self::class), E_USER_DEPRECATED);

$this->offset = func_get_arg(0);
if (func_num_args() > 1) {
$this->totalNumberOfLines = func_get_arg(1);
}
if (func_num_args() > 2) {
$this->skippedLineNumbers = func_get_arg(2);
}
}
}

/**
Expand Down Expand Up @@ -384,7 +385,11 @@ private function parseBlock($offset, $yaml, $flags)
$skippedLineNumbers[] = $lineNumber;
}

$parser = new self($offset, $this->totalNumberOfLines, $skippedLineNumbers);
$parser = new self();
$parser->offset = $offset;
$parser->totalNumberOfLines = $this->totalNumberOfLines;
$parser->skippedLineNumbers = $skippedLineNumbers;

$parser->refs = &$this->refs;

return $parser->parse($yaml, $flags);
Expand Down