@@ -34,6 +34,13 @@ final class Dependency_Update {
3434 */
3535 private static $ runner ;
3636
37+ /**
38+ * Runs `composer why-not`, or null for the real one. Tests swap it out.
39+ *
40+ * @var callable|null
41+ */
42+ private static $ why_not_runner ;
43+
3744 /**
3845 * Replaces the `composer update` run, for tests.
3946 *
@@ -46,6 +53,19 @@ public static function set_runner( $runner ) {
4653 self ::$ runner = $ runner ;
4754 }
4855
56+ /**
57+ * Replaces the `composer why-not` run, for tests.
58+ *
59+ * @param callable|null $runner Receives the contents of composer.json and
60+ * composer.lock, a package, a version and the
61+ * branch, and returns what holds that version
62+ * back, as for get_held_back_by(). Null
63+ * restores the real run.
64+ */
65+ public static function set_why_not_runner ( $ runner ) {
66+ self ::$ why_not_runner = $ runner ;
67+ }
68+
4969 /**
5070 * Works out what an update of the default branch of a repository changes.
5171 *
@@ -59,7 +79,10 @@ public static function set_runner( $runner ) {
5979 * `branch` is the default branch and `head` the branch for the
6080 * pull request. `unmatched` lists the given packages that match no
6181 * locked package. `composer_lock` holds the new lock, or null when
62- * the update changes no locked version.
82+ * the update changes no locked version. Each of the `remaining`
83+ * advisories also has a `fixed_in` version, the first one that
84+ * fixes it, and `held_back_by`, what `composer why-not` says keeps
85+ * that version out, see explain().
6386 */
6487 public static function prepare ( $ repo , array $ packages = [] ) {
6588 $ files = Dependency_Audit ::fetch_files ( $ repo );
@@ -68,6 +91,165 @@ public static function prepare( $repo, array $packages = [] ) {
6891 return null ;
6992 }
7093
94+ $ plan = self ::plan ( $ repo , $ packages , $ files );
95+
96+ $ plan ['remaining ' ] = self ::explain (
97+ $ plan ['remaining ' ],
98+ $ files ['composer_json ' ],
99+ null === $ plan ['composer_lock ' ] ? $ files ['composer_lock ' ] : $ plan ['composer_lock ' ],
100+ $ files ['ref ' ]
101+ );
102+
103+ return $ plan ;
104+ }
105+
106+ /**
107+ * Adds to each advisory the first version that fixes it, and what holds
108+ * that version back.
109+ *
110+ * @param array $advisories Advisories, see Dependency_Audit.
111+ * @param string $composer_json Contents of composer.json.
112+ * @param string $composer_lock Contents of the composer.lock they apply to.
113+ * @param string $branch Branch the files are from.
114+ *
115+ * @return array The advisories, each with `fixed_in`, null when no fixed
116+ * version is known, and `held_back_by`, the reasons
117+ * `composer why-not` gives, an empty list when it finds none,
118+ * or null when it was not asked.
119+ */
120+ public static function explain ( array $ advisories , $ composer_json , $ composer_lock , $ branch ) {
121+ $ runner = self ::$ why_not_runner ? self ::$ why_not_runner : [ __CLASS__ , 'get_held_back_by ' ];
122+ $ reasons = [];
123+
124+ foreach ( $ advisories as $ index => $ advisory ) {
125+ $ fixed_in = self ::get_first_fixed_version ( isset ( $ advisory ['affected_versions ' ] ) ? $ advisory ['affected_versions ' ] : null , $ advisory ['version ' ] );
126+ $ held_back_by = null ;
127+
128+ if ( null !== $ fixed_in ) {
129+ $ key = "{$ advisory ['package ' ]} {$ fixed_in }" ;
130+
131+ if ( ! array_key_exists ( $ key , $ reasons ) ) {
132+ $ reasons [ $ key ] = call_user_func ( $ runner , $ composer_json , $ composer_lock , $ advisory ['package ' ], $ fixed_in , $ branch );
133+ }
134+
135+ $ held_back_by = $ reasons [ $ key ];
136+ }
137+
138+ $ advisories [ $ index ]['fixed_in ' ] = $ fixed_in ;
139+ $ advisories [ $ index ]['held_back_by ' ] = $ held_back_by ;
140+ }
141+
142+ return $ advisories ;
143+ }
144+
145+ /**
146+ * Finds the lowest version above a locked one that none of the affected
147+ * ranges of an advisory covers. Advisories often list one range per
148+ * minor version, as in `>=5.3.0,<5.4.0|>=5.4.0,<5.4.56`, so the upper
149+ * bound of one range can be the start of the next.
150+ *
151+ * @param string|null $affected Affected versions, as Composer reports them.
152+ * @param string|null $version Locked version.
153+ *
154+ * @return string|null The version, or null when none is known to be fixed
155+ * or the ranges cannot be read.
156+ */
157+ public static function get_first_fixed_version ( $ affected , $ version ) {
158+ if ( ! is_string ( $ affected ) || ! is_string ( $ version ) || ! preg_match ( '/^v?(\d[\w.\-]*)$/i ' , $ version , $ match ) ) {
159+ return null ;
160+ }
161+
162+ $ ranges = [];
163+ foreach ( explode ( '| ' , $ affected ) as $ constraint ) {
164+ $ range = [
165+ 'lower ' => null ,
166+ 'upper ' => null ,
167+ ];
168+
169+ foreach ( preg_split ( '/[\s,]+/ ' , trim ( $ constraint ), -1 , PREG_SPLIT_NO_EMPTY ) as $ part ) {
170+ if ( ! preg_match ( '/^(>=|>|<=|<|==|=)?v?(\d[\w.\-]*)$/i ' , $ part , $ bound ) ) {
171+ return null ;
172+ }
173+
174+ if ( '' === $ bound [1 ] || '= ' === $ bound [1 ][0 ] ) {
175+ $ range ['lower ' ] = [ $ bound [2 ], true ];
176+ $ range ['upper ' ] = [ $ bound [2 ], true ];
177+ } elseif ( '> ' === $ bound [1 ][0 ] ) {
178+ $ range ['lower ' ] = [ $ bound [2 ], '>= ' === $ bound [1 ] ];
179+ } else {
180+ $ range ['upper ' ] = [ $ bound [2 ], '<= ' === $ bound [1 ] ];
181+ }
182+ }
183+
184+ $ ranges [] = $ range ;
185+ }
186+
187+ $ candidate = $ match [1 ];
188+ $ passes = count ( $ ranges ) + 1 ;
189+
190+ // Each pass but the last moves past a range, so this many will do.
191+ for ( $ pass = 0 ; $ pass < $ passes ; $ pass ++ ) {
192+ $ containing = null ;
193+ foreach ( $ ranges as $ range ) {
194+ if ( self ::in_range ( $ candidate , $ range ) ) {
195+ $ containing = $ range ;
196+ break ;
197+ }
198+ }
199+
200+ if ( null === $ containing ) {
201+ return $ candidate === $ match [1 ] ? null : $ candidate ;
202+ }
203+
204+ if ( null === $ containing ['upper ' ] || $ containing ['upper ' ][1 ] ) {
205+ return null ;
206+ }
207+
208+ $ candidate = $ containing ['upper ' ][0 ];
209+ }
210+
211+ return null ;
212+ }
213+
214+ /**
215+ * Asks `composer why-not` what keeps a version of a package from being
216+ * locked.
217+ *
218+ * @param string $composer_json Contents of composer.json.
219+ * @param string $composer_lock Contents of composer.lock.
220+ * @param string $package Package name.
221+ * @param string $version Version that should be locked.
222+ * @param string $branch Branch the files are from.
223+ *
224+ * @return string[]|null The reasons, one per line, such as
225+ * `symfony/yaml v6.4.50 requires php (>=8.1 but 7.2.24 is installed)`;
226+ * none when it finds nothing, and null when it fails.
227+ */
228+ public static function get_held_back_by ( $ composer_json , $ composer_lock , $ package , $ version , $ branch ) {
229+ $ result = Composer ::run (
230+ [ 'why-not ' , $ package , $ version , '--locked ' ],
231+ [
232+ 'composer.json ' => $ composer_json ,
233+ 'composer.lock ' => $ composer_lock ,
234+ ],
235+ [],
236+ [ 'COMPOSER_ROOT_VERSION ' => "dev- {$ branch }" ]
237+ );
238+
239+ // It exits with 1 when it finds something that holds the version back.
240+ if ( 0 === $ result ['return_code ' ] ) {
241+ return [];
242+ }
243+
244+ if ( 1 !== $ result ['return_code ' ] ) {
245+ WP_CLI ::debug ( "`composer why-not {$ package } {$ version }` failed (exit code {$ result ['return_code ' ]}). " , 'composer ' );
246+ return null ;
247+ }
248+
249+ return array_values ( array_filter ( array_map ( 'trim ' , explode ( "\n" , $ result ['stdout ' ] ) ), 'strlen ' ) );
250+ }
251+
252+ private static function plan ( $ repo , array $ packages , array $ files ) {
71253 $ before = self ::audit ( $ repo , $ files , $ files ['composer_lock ' ], $ files ['lock ' ] );
72254
73255 $ plan = [
@@ -307,6 +489,10 @@ public static function describe_pull_request( array $plan ) {
307489 $ lines [] = '' ;
308490 foreach ( $ plan ['remaining ' ] as $ advisory ) {
309491 $ lines [] = self ::describe_advisory ( $ advisory );
492+
493+ foreach ( self ::describe_held_back_by ( $ advisory ) as $ line ) {
494+ $ lines [] = '' === $ line ? '' : " {$ line }" ;
495+ }
310496 }
311497 }
312498
@@ -487,6 +673,43 @@ private static function describe_version( array $package, $show_reference ) {
487673 return sprintf ( '%s (%s) ' , $ package ['version ' ], substr ( $ package ['reference ' ], 0 , 7 ) );
488674 }
489675
676+ /**
677+ * Says what keeps the fix of an advisory out of the lock, as Markdown.
678+ *
679+ * @param array $advisory Advisory, see explain().
680+ *
681+ * @return string[] Lines.
682+ */
683+ private static function describe_held_back_by ( array $ advisory ) {
684+ if ( empty ( $ advisory ['fixed_in ' ] ) ) {
685+ return [];
686+ }
687+
688+ $ fixed_in = $ advisory ['fixed_in ' ];
689+
690+ if ( null === $ advisory ['held_back_by ' ] ) {
691+ return [ "Fixed in {$ fixed_in }. " ];
692+ }
693+
694+ if ( ! $ advisory ['held_back_by ' ] ) {
695+ return [ "Fixed in {$ fixed_in }. `composer why-not` finds nothing that holds it back; `composer update \"{$ advisory ['package ' ]}: {$ fixed_in }\" --dry-run` may tell more. " ];
696+ }
697+
698+ return array_merge (
699+ [ "Fixed in {$ fixed_in }, which `composer why-not` says is held back by: " , '' , '``` ' ],
700+ $ advisory ['held_back_by ' ],
701+ [ '``` ' ]
702+ );
703+ }
704+
705+ private static function in_range ( $ version , array $ range ) {
706+ if ( null !== $ range ['lower ' ] && ! version_compare ( $ version , $ range ['lower ' ][0 ], $ range ['lower ' ][1 ] ? '>= ' : '> ' ) ) {
707+ return false ;
708+ }
709+
710+ return null === $ range ['upper ' ] || version_compare ( $ version , $ range ['upper ' ][0 ], $ range ['upper ' ][1 ] ? '<= ' : '< ' );
711+ }
712+
490713 private static function describe_advisory ( array $ advisory ) {
491714 $ id = $ advisory ['link ' ] ? "[ {$ advisory ['advisory ' ]}]( {$ advisory ['link ' ]}) " : $ advisory ['advisory ' ];
492715
0 commit comments