<table role="table">
<thead>
<tr>
<th>Q</th>
<th>A</th>
</tr>
</thead>
<tbody>
<tr>
<td>Bug report?</td>
<td>no</td>
</tr>
<tr>
<td>Feature request?</td>
<td>no</td>
</tr>
<tr>
<td>BC Break report?</td>
<td>no</td>
</tr>
<tr>
<td>RFC?</td>
<td>yes</td>
</tr>
<tr>
<td>Symfony version</td>
<td>3.4</td>
</tr>
</tbody>
</table>
<p dir="auto">While the Symfony DI component is a really awesome tool to help build high quality apps, I was thinking there could be ways to improve its DX in terms of autocompletion in IDEs.</p>
<p dir="auto">When interacting with service locators, we are used to do things like <code class="notranslate">$this-&gt;get('my_service')</code>, or more recently <code class="notranslate">$this-&gt;get(MyService::class)</code>. Without a Symfony plugin, IDEs are not able to autocomplete what comes after <code class="notranslate">$this-&gt;get('my_service')</code> as the type of the returned element is not known until runtime.</p>
<p dir="auto">After some dicussions with <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/nicolas-grekas/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/nicolas-grekas">@nicolas-grekas</a>, we came up with the idea of a ServiceSubscriberTrait that could be used to improve DX on this level. Here is an example of usage (the class would be userland, the trait would be in Symfony):</p>
<p dir="auto"><a href="https://gist.github.com/tgalopin/46e2be955762d6d68360dd455afeab70">https://gist.github.com/tgalopin/46e2be955762d6d68360dd455afeab70</a></p>
<p dir="auto">The idea is to detect in <code class="notranslate">getSubscribedServices</code> all the current class methods and inject automatically the right services in the service locator using the typehints. This would mean developers could autocomplete usages of services by implementing tiny helper methods, relying on the trait to detect what they need based on these methods.</p>
<p dir="auto">I would like to get opinions about this before trying to work on an implementation. WDYT?</p>
<p dir="auto">I would like to use the Validator component to validate objects against a set of rules I need to create programatically.</p>
<p dir="auto">This validation needs to be applied „stand-alone“ (for example, when populating objects with data provided as JSON). If it could also transparently be used when working with Forms that would be even better.</p>
<p dir="auto">Basically, it would be great if I could write something similar to</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="$validator-&gt;validate($someObject, array(
    new ThisConstraint(…some property path, options ...),
    new ThatConstraint(… some other property path, options ...)
));"><pre class="notranslate"><span class="pl-s1"><span class="pl-c1">$</span>validator</span>-&gt;<span class="pl-en">validate</span>(<span class="pl-s1"><span class="pl-c1">$</span>someObject</span>, <span class="pl-en">array</span>(
    <span class="pl-k">new</span> <span class="pl-v">ThisConstraint</span>(…some property path, options .<span class="pl-c1">.</span>.),
    <span class="pl-k">new</span> <span class="pl-v">ThatConstraint</span>(… some other property path, options .<span class="pl-c1">.</span>.)
));</pre></div>
<p dir="auto">One example that shows some of the problems I see is a simple DateTime object that I would like to validate as a „FutureDate“ or „PastDate“.</p>
<p dir="auto">Obviously, I cannot use annotations to define these constraints on the <code class="notranslate">DateTime</code>.</p>
<p dir="auto">The „PHP“ way of defining annotations via a method on the class (see <a href="http://symfony.com/doc/current/book/validation.html#constraint-configuration" rel="nofollow">here</a>) in question also does not work unless I'm subclassing.</p>
<p dir="auto">Defining two validation groups in YAML or XML for the DateTime class might work, but I think would abuse the idea of validation groups. Additionally, how could I validate an object containing two DateTimes and applying FutureDate and PastDate respectively?</p>
<p dir="auto">And how could I define the constraints if they need to be based on user input?</p>
<p dir="auto">I really hope that I am overlooking some (maybe little or undocumented) flex points in the API and would be glad if someone could give me starting pointers.</p>
<p dir="auto">Will improve docs in return if it works out :-)!</p>