1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
//! ref: composer/src/Composer/Package/AliasPackage.php
use chrono::{DateTime, Utc};
use indexmap::IndexMap;
use shirabe_php_shim::{PhpMixed, in_array};
use shirabe_semver::constraint::Constraint;
use crate::package::BasePackage;
use crate::package::Link;
use crate::package::PackageInterface;
use crate::package::version::VersionParser;
use crate::repository::RepositoryInterface;
#[derive(Debug)]
pub struct AliasPackage {
id: i64,
name: String,
pretty_name: String,
repository: Option<Box<dyn RepositoryInterface>>,
/// @var string
pub(crate) version: String,
/// @var string
pub(crate) pretty_version: String,
/// @var bool
pub(crate) dev: bool,
/// @var bool
pub(crate) root_package_alias: bool,
/// @var string
/// @phpstan-var 'stable'|'RC'|'beta'|'alpha'|'dev'
pub(crate) stability: String,
/// @var bool
pub(crate) has_self_version_requires: bool,
/// @var BasePackage
pub(crate) alias_of: Box<dyn BasePackage>,
/// @var Link[]
pub(crate) requires: IndexMap<String, Link>,
/// @var Link[]
pub(crate) dev_requires: IndexMap<String, Link>,
/// @var array<string, Link>
pub(crate) conflicts: IndexMap<String, Link>,
/// @var array<string, Link>
pub(crate) provides: IndexMap<String, Link>,
/// @var array<string, Link>
pub(crate) replaces: IndexMap<String, Link>,
}
impl AliasPackage {
/// All descendants' constructors should call this parent constructor
///
/// @param BasePackage $aliasOf The package this package is an alias of
/// @param string $version The version the alias must report
/// @param string $prettyVersion The alias's non-normalized version
pub fn new(alias_of: Box<dyn BasePackage>, version: String, pretty_version: String) -> Self {
let alias_name = alias_of.get_name().to_string();
let stability = VersionParser::parse_stability(&version).to_string();
let dev = stability == "dev";
let mut this = Self {
id: -1,
name: alias_name.to_lowercase(),
pretty_name: alias_name,
repository: None,
version,
pretty_version,
dev,
root_package_alias: false,
stability,
has_self_version_requires: false,
alias_of,
requires: IndexMap::new(),
dev_requires: IndexMap::new(),
conflicts: IndexMap::new(),
provides: IndexMap::new(),
replaces: IndexMap::new(),
};
for r#type in Link::types() {
// PHP: $aliasOf->{'get' . ucfirst($type)}()
// TODO(phase-b): dynamic method dispatch — bridge each Link::TYPE_* to BasePackage getter
let links: Vec<Link> = match r#type {
Link::TYPE_REQUIRE => this.alias_of.get_requires().values().cloned().collect(),
Link::TYPE_DEV_REQUIRE => {
this.alias_of.get_dev_requires().values().cloned().collect()
}
Link::TYPE_PROVIDE => this.alias_of.get_provides().values().cloned().collect(),
Link::TYPE_CONFLICT => this.alias_of.get_conflicts().values().cloned().collect(),
Link::TYPE_REPLACE => this.alias_of.get_replaces().values().cloned().collect(),
_ => vec![],
};
let replaced = this.replace_self_version_dependencies(links, r#type);
match r#type {
Link::TYPE_REQUIRE => {
this.requires = replaced
.into_iter()
.map(|l| (l.get_target().to_string(), l))
.collect();
}
Link::TYPE_DEV_REQUIRE => {
this.dev_requires = replaced
.into_iter()
.map(|l| (l.get_target().to_string(), l))
.collect();
}
Link::TYPE_PROVIDE => {
this.provides = replaced
.into_iter()
.map(|l| (l.get_target().to_string(), l))
.collect()
}
Link::TYPE_CONFLICT => {
this.conflicts = replaced
.into_iter()
.map(|l| (l.get_target().to_string(), l))
.collect()
}
Link::TYPE_REPLACE => {
this.replaces = replaced
.into_iter()
.map(|l| (l.get_target().to_string(), l))
.collect()
}
_ => {}
}
}
this
}
pub fn get_alias_of(&self) -> &dyn BasePackage {
self.alias_of.as_ref()
}
pub fn get_alias_of_mut(&mut self) -> &mut dyn BasePackage {
&mut *self.alias_of
}
/// Stores whether this is an alias created by an aliasing in the requirements of the root package or not
///
/// Use by the policy for sorting manually aliased packages first, see #576
pub fn set_root_package_alias(&mut self, value: bool) {
self.root_package_alias = value;
}
/// @see setRootPackageAlias
pub fn is_root_package_alias(&self) -> bool {
self.root_package_alias
}
/// @param Link[] $links
/// @param Link::TYPE_* $linkType
///
/// @return Link[]
pub(crate) fn replace_self_version_dependencies(
&mut self,
mut links: Vec<Link>,
link_type: &str,
) -> Vec<Link> {
// for self.version requirements, we use the original package's branch name instead, to avoid leaking the magic dev-master-alias to users
let mut pretty_version = self.pretty_version.clone();
if pretty_version == VersionParser::DEFAULT_BRANCH_ALIAS {
pretty_version = self.alias_of.get_pretty_version().to_string();
}
if in_array(
PhpMixed::String(link_type.to_string()),
&PhpMixed::List(vec![
Box::new(PhpMixed::String(Link::TYPE_CONFLICT.to_string())),
Box::new(PhpMixed::String(Link::TYPE_PROVIDE.to_string())),
Box::new(PhpMixed::String(Link::TYPE_REPLACE.to_string())),
]),
true,
) {
let mut new_links: Vec<Link> = vec![];
for link in &links {
// link is self.version, but must be replacing also the replaced version
if link.get_pretty_constraint().unwrap_or("") == "self.version" {
let mut constraint = Constraint::new("=", &self.version);
let new_link = Link::new(
link.get_source().to_string(),
link.get_target().to_string(),
Box::new(constraint.clone()),
Some(link_type.to_string()),
Some(pretty_version.clone()),
);
shirabe_semver::constraint::ConstraintInterface::set_pretty_string(
&mut constraint,
Some(pretty_version.clone()),
);
new_links.push(new_link);
}
}
links.extend(new_links);
} else {
// PHP: foreach ($links as $index => $link)
for index in 0..links.len() {
if links[index].get_pretty_constraint().unwrap_or("") == "self.version" {
if link_type == Link::TYPE_REQUIRE {
self.has_self_version_requires = true;
}
let mut constraint = Constraint::new("=", &self.version);
let new_link = Link::new(
links[index].get_source().to_string(),
links[index].get_target().to_string(),
Box::new(constraint.clone()),
Some(link_type.to_string()),
Some(pretty_version.clone()),
);
shirabe_semver::constraint::ConstraintInterface::set_pretty_string(
&mut constraint,
Some(pretty_version.clone()),
);
links[index] = new_link;
}
}
}
links
}
pub fn has_self_version_requires(&self) -> bool {
self.has_self_version_requires
}
}
impl std::fmt::Display for AliasPackage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} ({}alias of {})",
self.alias_of,
if self.root_package_alias { "root " } else { "" },
self.alias_of.get_version(),
)
}
}
impl PackageInterface for AliasPackage {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn get_name(&self) -> &str {
self.alias_of.get_name()
}
fn get_pretty_name(&self) -> &str {
self.alias_of.get_pretty_name()
}
fn get_names(&self, provides: bool) -> Vec<String> {
self.alias_of.get_names(provides)
}
fn set_id(&mut self, id: i64) {
self.alias_of.set_id(id);
}
fn get_id(&self) -> i64 {
self.alias_of.get_id()
}
fn is_dev(&self) -> bool {
self.dev
}
fn get_version(&self) -> &str {
&self.version
}
fn get_stability(&self) -> &str {
&self.stability
}
fn get_pretty_version(&self) -> &str {
&self.pretty_version
}
fn get_requires(&self) -> IndexMap<String, Link> {
self.requires.clone()
}
/// @inheritDoc
/// @return array<string, Link>
fn get_conflicts(&self) -> IndexMap<String, Link> {
self.conflicts.clone()
}
/// @inheritDoc
/// @return array<string, Link>
fn get_provides(&self) -> IndexMap<String, Link> {
self.provides.clone()
}
/// @inheritDoc
/// @return array<string, Link>
fn get_replaces(&self) -> IndexMap<String, Link> {
self.replaces.clone()
}
fn get_dev_requires(&self) -> IndexMap<String, Link> {
self.dev_requires.clone()
}
fn get_type(&self) -> &str {
self.alias_of.get_type()
}
fn get_target_dir(&self) -> Option<&str> {
self.alias_of.get_target_dir()
}
fn get_extra(&self) -> IndexMap<String, PhpMixed> {
self.alias_of.get_extra()
}
fn set_installation_source(&mut self, r#type: Option<String>) {
self.alias_of.set_installation_source(r#type);
}
fn get_installation_source(&self) -> Option<&str> {
self.alias_of.get_installation_source()
}
fn get_source_type(&self) -> Option<&str> {
self.alias_of.get_source_type()
}
fn get_source_url(&self) -> Option<&str> {
self.alias_of.get_source_url()
}
fn get_source_urls(&self) -> Vec<String> {
self.alias_of.get_source_urls()
}
fn get_source_reference(&self) -> Option<&str> {
self.alias_of.get_source_reference()
}
fn set_source_reference(&mut self, reference: Option<String>) {
self.alias_of.set_source_reference(reference);
}
fn set_source_mirrors(&mut self, mirrors: Option<Vec<IndexMap<String, PhpMixed>>>) {
self.alias_of.set_source_mirrors(mirrors);
}
fn get_source_mirrors(&self) -> Option<Vec<IndexMap<String, PhpMixed>>> {
self.alias_of.get_source_mirrors()
}
fn get_dist_type(&self) -> Option<&str> {
self.alias_of.get_dist_type()
}
fn get_dist_url(&self) -> Option<&str> {
self.alias_of.get_dist_url()
}
fn get_dist_urls(&self) -> Vec<String> {
self.alias_of.get_dist_urls()
}
fn get_dist_reference(&self) -> Option<&str> {
self.alias_of.get_dist_reference()
}
fn set_dist_reference(&mut self, reference: Option<String>) {
self.alias_of.set_dist_reference(reference);
}
fn get_dist_sha1_checksum(&self) -> Option<&str> {
self.alias_of.get_dist_sha1_checksum()
}
fn set_transport_options(&mut self, options: IndexMap<String, PhpMixed>) {
self.alias_of.set_transport_options(options);
}
fn get_transport_options(&self) -> IndexMap<String, PhpMixed> {
self.alias_of.get_transport_options()
}
fn set_dist_mirrors(&mut self, mirrors: Option<Vec<IndexMap<String, PhpMixed>>>) {
self.alias_of.set_dist_mirrors(mirrors);
}
fn get_dist_mirrors(&self) -> Option<Vec<IndexMap<String, PhpMixed>>> {
self.alias_of.get_dist_mirrors()
}
fn get_autoload(&self) -> IndexMap<String, PhpMixed> {
self.alias_of.get_autoload()
}
fn get_dev_autoload(&self) -> IndexMap<String, PhpMixed> {
self.alias_of.get_dev_autoload()
}
fn get_include_paths(&self) -> Vec<String> {
self.alias_of.get_include_paths()
}
fn get_php_ext(&self) -> Option<IndexMap<String, PhpMixed>> {
self.alias_of.get_php_ext()
}
fn get_release_date(&self) -> Option<DateTime<Utc>> {
self.alias_of.get_release_date()
}
fn get_binaries(&self) -> Vec<String> {
self.alias_of.get_binaries()
}
fn get_suggests(&self) -> IndexMap<String, String> {
self.alias_of.get_suggests()
}
fn get_notification_url(&self) -> Option<&str> {
self.alias_of.get_notification_url()
}
fn is_default_branch(&self) -> bool {
self.alias_of.is_default_branch()
}
fn set_dist_url(&mut self, url: Option<String>) {
self.alias_of.set_dist_url(url);
}
fn set_dist_type(&mut self, r#type: Option<String>) {
self.alias_of.set_dist_type(r#type);
}
fn set_source_dist_references(&mut self, reference: &str) {
self.alias_of.set_source_dist_references(reference);
}
fn get_full_pretty_version(&self, truncate: bool, display_mode: i64) -> String {
// TODO(phase-b): BasePackage.get_full_pretty_version returns Result; bridge here
BasePackage::get_full_pretty_version(self.alias_of.as_ref(), truncate, display_mode)
.unwrap_or_default()
}
fn get_unique_name(&self) -> String {
self.alias_of.get_unique_name()
}
fn get_pretty_string(&self) -> String {
self.alias_of.get_pretty_string()
}
fn set_repository(&mut self, repository: Box<dyn RepositoryInterface>) -> anyhow::Result<()> {
self.alias_of.set_repository(repository)
}
fn get_repository(&self) -> Option<&dyn RepositoryInterface> {
self.alias_of.get_repository()
}
}
impl BasePackage for AliasPackage {
fn id(&self) -> i64 {
self.id
}
fn id_mut(&mut self) -> &mut i64 {
&mut self.id
}
fn name(&self) -> &str {
&self.name
}
fn name_mut(&mut self) -> &mut String {
&mut self.name
}
fn pretty_name(&self) -> &str {
&self.pretty_name
}
fn pretty_name_mut(&mut self) -> &mut String {
&mut self.pretty_name
}
fn repository_opt(&self) -> Option<&dyn RepositoryInterface> {
self.repository.as_deref()
}
fn set_repository_box(&mut self, repository: Box<dyn RepositoryInterface>) {
todo!()
}
fn take_repository(&mut self) -> Option<Box<dyn RepositoryInterface>> {
todo!()
}
fn clone_box(&self) -> Box<dyn BasePackage> {
todo!()
}
}
|