Skip to content

[Finder] Add documentation and code on array as method parameter #10038

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

Conversation

jfredon
Copy link
Contributor

@jfredon jfredon commented Jul 9, 2018

@jfredon jfredon changed the title [WIP][WCM][Finder] Add documentation and code on array as method parameter [WCM][Finder] Add documentation and code on array as method parameter Jul 10, 2018
The ``notName()`` method excludes files matching a pattern::

$finder->files()->notName('*.rb');

Multiple files names can be excluded by chaining calls or using an array as parameter::

$finder->files()->notName('*.rb')->name('*.py');
Copy link
Member

Choose a reason for hiding this comment

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

->name('*.py') should be ->notName('*.py'), right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right. It’s fixed.

@javiereguiluz javiereguiluz added the Waiting Code Merge Docs for features pending to be merged label Jul 10, 2018
@jfredon jfredon force-pushed the finder-documents-array-as-method-parameter branch from 2bf71e1 to 361dde5 Compare July 10, 2018 08:32
fabpot added a commit to symfony/symfony that referenced this pull request Jul 16, 2018
…r better fluent experience and code readability (jfredon)

This PR was squashed before being merged into the 4.2-dev branch (closes #27891).

Discussion
----------

[Finder] Allow arrays as parameters of some methods for better fluent experience and code readability

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | none
| License       | MIT
| Doc PR        | symfony/symfony-docs#10038

Makes the Finder library a little more convenient to use by allowing the use of arrays as parameters of methods that can be called multiple times.

This way of doing things was already present for the `Finder::in()` and `Finder::exclude()` methods, it has been extended to other methods that can be called several times to cumulate their effects.
This allows a better use of fluent methods by avoiding breaking the chaining to iterate on array variables (a little more complexity in the Finder library for less complexity in applications that uses it).

```php
// we could use
$finder = Finder::create()->in($fileRepository)->name($fileList);

// instead of
$finder = Finder::create()->in($fileRepository);
foreach ($fileList as $file) {
    $finder->name($file);
}
```

In `.php_cs` files, this would make the code more readable by separating the configuration of the execution code:

```php
<?php
const RULES = [
    '@symfony' => true,
    '@symfony:risky' => true,
];
const EXCLUDED_DIRS = [
    // directories containing files with content that is autogenerated by `var_export`, which breaks CS in output code
    'Symfony/Component/Cache/Tests/Marshaller/Fixtures',
    'Symfony/Component/DependencyInjection/Tests/Fixtures',
    // ...
];
const EXCLUDED_FILES = [
    // file content autogenerated by `var_export`
    'Symfony/Component/Translation/Tests/fixtures/resources.php',
    // test template
    'Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Resources/Custom/_name_entry_label.html.php',
    // ...
];

return PhpCsFixer\Config::create()
    ->setRules(RULES)
    ->setRiskyAllowed(true)
    ->setFinder(
        PhpCsFixer\Finder::create()
            ->in(__DIR__.'/src')
            ->append(array(__FILE__))
            ->exclude(EXCLUDED_DIRS)
            ->notPath(EXCLUDED_FILES)
    )
;
```

TODO
- [x] complete the tests to validate the new syntax on all modified methods
- [x] submit changes to the Finder documentation

Commits
-------

ad97cd7 [Finder] Allow arrays as parameters of some methods for better fluent experience and code readability
@jfredon jfredon changed the title [WCM][Finder] Add documentation and code on array as method parameter [Finder] Add documentation and code on array as method parameter Jul 16, 2018
@javiereguiluz javiereguiluz added Status: Reviewed and removed Waiting Code Merge Docs for features pending to be merged Status: Needs Review labels Jul 25, 2018
@javiereguiluz javiereguiluz added this to the 4.2 milestone Jul 25, 2018
@javiereguiluz
Copy link
Member

@jfredon thanks a lot for implementing this nice feature ... and for contributing its docs too!

@javiereguiluz javiereguiluz merged commit 361dde5 into symfony:master Jul 25, 2018
javiereguiluz added a commit that referenced this pull request Jul 25, 2018
…arameter (jfredon)

This PR was merged into the master branch.

Discussion
----------

[Finder] Add documentation and code on array as method parameter

Documents symfony/symfony#27891

Commits
-------

361dde5 [Finder] Add documentation and code on array as method parameter
xabbuh added a commit that referenced this pull request Aug 6, 2018
xabbuh added a commit that referenced this pull request Aug 6, 2018
Liinkiing pushed a commit to Liinkiing/symfony-docs that referenced this pull request Aug 24, 2018
Liinkiing pushed a commit to Liinkiing/symfony-docs that referenced this pull request Aug 24, 2018
Liinkiing pushed a commit to Liinkiing/symfony-docs that referenced this pull request Aug 24, 2018
Liinkiing pushed a commit to Liinkiing/symfony-docs that referenced this pull request Aug 24, 2018
joshtrichards pushed a commit to joshtrichards/symfony-finder that referenced this pull request Apr 26, 2024
…r better fluent experience and code readability (jfredon)

This PR was squashed before being merged into the 4.2-dev branch (closes #27891).

Discussion
----------

[Finder] Allow arrays as parameters of some methods for better fluent experience and code readability

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | none
| License       | MIT
| Doc PR        | symfony/symfony-docs#10038

Makes the Finder library a little more convenient to use by allowing the use of arrays as parameters of methods that can be called multiple times.

This way of doing things was already present for the `Finder::in()` and `Finder::exclude()` methods, it has been extended to other methods that can be called several times to cumulate their effects.
This allows a better use of fluent methods by avoiding breaking the chaining to iterate on array variables (a little more complexity in the Finder library for less complexity in applications that uses it).

```php
// we could use
$finder = Finder::create()->in($fileRepository)->name($fileList);

// instead of
$finder = Finder::create()->in($fileRepository);
foreach ($fileList as $file) {
    $finder->name($file);
}
```

In `.php_cs` files, this would make the code more readable by separating the configuration of the execution code:

```php
<?php
const RULES = [
    '@symfony' => true,
    '@symfony:risky' => true,
];
const EXCLUDED_DIRS = [
    // directories containing files with content that is autogenerated by `var_export`, which breaks CS in output code
    'Symfony/Component/Cache/Tests/Marshaller/Fixtures',
    'Symfony/Component/DependencyInjection/Tests/Fixtures',
    // ...
];
const EXCLUDED_FILES = [
    // file content autogenerated by `var_export`
    'Symfony/Component/Translation/Tests/fixtures/resources.php',
    // test template
    'Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Resources/Custom/_name_entry_label.html.php',
    // ...
];

return PhpCsFixer\Config::create()
    ->setRules(RULES)
    ->setRiskyAllowed(true)
    ->setFinder(
        PhpCsFixer\Finder::create()
            ->in(__DIR__.'/src')
            ->append(array(__FILE__))
            ->exclude(EXCLUDED_DIRS)
            ->notPath(EXCLUDED_FILES)
    )
;
```

TODO
- [x] complete the tests to validate the new syntax on all modified methods
- [x] submit changes to the Finder documentation

Commits
-------

6006300 [Finder] Allow arrays as parameters of some methods for better fluent experience and code readability
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants